.net framework
1. 如何獲得系統文件夾
使用system.envioment類的getfolderpath方法;例如:
environment.getfolderpath( environment.specialfolder.personal )
2. 如何獲得正在執行的exe文件的路徑
1) 使用application類的executablepath屬性
2) system.reflection.assembly.getexecutingassembly().location
3. 如何檢測操作系統的版本
使用envioment的osversion屬性,例如:
operatingsystem os = environment.osversion;
messagebox.show(os.version.tostring());
messagebox.show(os.platform.tostring());
4. 如何根據完整的文件名獲得文件的文件名部分
使用system.io.path類的方法getfilename或者getfilenamewithoutextension方法
5. 如何通過文件的全名獲得文件的擴展名
使用system.io.path.getextension靜態方法
6. vb和c#的語法有什么不同click here
7. 如何獲得當前電腦用戶名,是否聯網,幾個顯示器,所在域,鼠標有幾個鍵等信息
使用system.windows.forms. systeminformation類的靜態屬性
8. 修飾main方法的[stathread]特性有什么作用
標示當前程序使用單線程的方式運行
9. 如何讀取csv文件的內容
通過odbcconnection可以創建一個鏈接到csv文件的鏈接,鏈接字符串的格式是:"driver={microsoft text driver (*.txt;*.csv)};dbq="+cvs文件的文件夾路徑+" extensions=asc,csv,tab,txt; persist security info=false";
創建連接之后就可以使用dataadapter等存取csv文件了。
詳細信息見此處
10. 如何獲得磁盤開銷信息,代碼片斷如下,主要是調用kernel32.dll中的getdiskfreespaceex外部方法。
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 available, out total, out free);
}
public static driveinfosystem getinfo(string drive)
{
long result, available, total, free;
result = getdiskfreespaceex(drive, out available, out total, out free);
return new driveinfosystem(drive, result, available, total, free);
}
}
public struct driveinfosystem
{
public readonly string drive;
public readonly long result;
public readonly long available;
public readonly long total;
public readonly long free;
public driveinfosystem(string drive, long result, long available, long total, long free)
{
this.drive = drive;
this.result = result;
this.available = available;
this.total = total;
this.free = free;
}
}
可以通過driveinfosystem info = driveinfo.getinfo("c:");來獲得指定磁盤的開銷情況
11.如何獲得不區分大小寫的子字符串的索引位置
1)通過將兩個字符串轉換成小寫之后使用字符串的indexof方法:
string strparent = "the codeproject site is very informative.";
string strchild = "codeproject";
// the line below will return -1 when expected is 4.
int i = strparent.indexof(strchild);
// the line below will return proper index
int j = strparent.tolower().indexof(strchild.tolower());
2) 一種更優雅的方法是使用system.globalization命名空間下面的compareinfo類的indexof方法:
using system.globalization;
string strparent = "the codeproject site is very informative.";
string strchild = "codeproject";
// we create a 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);
國內最大的酷站演示中心!新聞熱點
疑難解答
圖片精選