asp.net some cool, practical. NET tips 1th/2 page _ Practical Tips

Source: Internet
Author: User
I.. Net Framework

1. How to obtain a system folder

Use the GetFolderPath method of the System.envioment class, for example:

Environment.getfolderpath (Environment.SpecialFolder.Personal)

2. How to get the path of an EXE file being executed

1 using the ExecutablePath property of the application class

2) System.Reflection.Assembly.GetExecutingAssembly (). Location

3. How to detect the version of the operating system

Use the OSVersion property of Envioment, for example:

OperatingSystem os = environment.osversion;

MessageBox.Show (OS. Version.tostring ());

MessageBox.Show (OS. Platform.tostring ());

4. How to obtain the file name portion of the document based on the full file name,

Methods for using the System.IO.Path class GetFileName or Getfilenamewithoutextension methods

5. How to obtain the file extension through the full name of the file

Using the System.IO.Path.GetExtension static method

6. What is the difference between VB and C # 's syntax click here

7. How to get the current computer user name, whether networking, several monitors, the domain, the mouse has several keys and other information

Use static properties of the System.Windows.Forms. SystemInformation class

8. What is the effect of the [STAThread] attribute of the modifier main method

To indicate that the current program is running in a single threaded way

9. How to read the contents of a CSV file

Through OdbcConnection, you can create a link to a CSV file that is formatted as: "Driver={microsoft Text Driver (*.txt;*.csv)};D bq=" +cvs file Folder path + "Ext Ensions=asc,csv,tab,txt; Persist security Info=false ";

After you create the connection, you can use DataAdapter to access the CSV file.

See here for more information

10. How to obtain the disk cost information, the code snippet is as follows, mainly call the GetDiskFreeSpaceEx external method in Kernel32.dll.




public sealed class DriveInfo
{
[DllImport ("kernel32.dll", EntryPoint = "Getdiskfreespaceexa")]
private static extern long GetDiskFreeSpaceEx (string lpdirectoryname,
Out Long Lpfreebytesavailabletocaller,
Out Long lptotalnumberofbytes,
Out long lptotalnumberoffreebytes);

public static long GetInfo (string drive, out long available, out long total, out long free)
{
return GetDiskFreeSpaceEx (drive, out of available, out total, out of free);
}

public static Driveinfosystem GetInfo (String drive)
{
Long result, available, total, free;
result = GetDiskFreeSpaceEx (drive, out available, out total, out free);
Return to new Driveinfosystem (drive, result, available, total, free);
}
}

public struct Driveinfosystem
{
Public ReadOnly string Drive;
Public ReadOnly long;
Public ReadOnly long Available;
Public ReadOnly long;
Public ReadOnly long Free;

Public Driveinfosystem (String drive, long, long available, long, long)
{
This. Drive = Drive;
This. result = result;
This. Available = Available;
This. Total = total;
This. free = free;
}
}




Can pass

Driveinfosystem info = driveinfo.getinfo ("C:") to get the cost of the specified disk


11. How to get the index position of a case-insensitive substring

1 IndexOf method that uses a string after converting two strings to lowercase:




String strparent = "The CodeProject site is very informative.";

String strchild = "CodeProject";

The line below would return-1 when expected is 4.
int i = Strparent.indexof (strchild);

The line below would return proper index
Int J = Strparent.tolower (). IndexOf (Strchild.tolower ());



2)

A more elegant approach is to use the IndexOf method of the CompareInfo class below the System.Globalization namespace:



Using System.Globalization;

String strparent = "The CodeProject site is very informative.";

String strchild = "CodeProject";
We create an object of CompareInfo class for a neutral culture or a culture insensitive object
CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;

int i = Compare.indexof (strparent,strchild,compareoptions.ignorecase);

Current 1/2 page 12 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.