asp.net一些很酷很實用的.Net技巧第1/2頁
更新時間:2008年08月04日 09:21:05 作者:
方便使用asp.net編程的朋友,都是一些非常有用的東西
一..Net Framework
1. 如何獲得系統(tǒng)文件夾
使用System.Envioment類的GetFolderPath方法;例如:
Environment.GetFolderPath( Environment.SpecialFolder.Personal )
2. 如何獲得正在執(zhí)行的exe文件的路徑
1) 使用Application類的ExecutablePath屬性
2) System.Reflection.Assembly.GetExecutingAssembly().Location
3. 如何檢測操作系統(tǒng)的版本
使用Envioment的OSVersion屬性,例如:
OperatingSystem os = Environment.OSVersion;
MessageBox.Show(os.Version.ToString());
MessageBox.Show(os.Platform.ToString());
4. 如何根據(jù)完整的文件名獲得文件的文件名部分、
使用System.IO.Path類的方法GetFileName或者GetFileNameWithoutExtension方法
5. 如何通過文件的全名獲得文件的擴展名
使用System.IO.Path.GetExtension靜態(tài)方法
6. Vb和c#的語法有什么不同click here
7. 如何獲得當前電腦用戶名,是否聯(lián)網(wǎng),幾個顯示器,所在域,鼠標有幾個鍵等信息
使用System.Windows.Forms. SystemInformation類的靜態(tài)屬性
8. 修飾Main方法的[STAThread]特性有什么作用
標示當前程序使用單線程的方式運行
9. 如何讀取csv文件的內(nèi)容
通過OdbcConnection可以創(chuàng)建一個鏈接到csv文件的鏈接,鏈接字符串的格式是:"Driver={Microsoft Text Driver (*.txt;*.csv)};Dbq="+cvs文件的文件夾路徑+" Extensions=asc,csv,tab,txt; Persist Security Info=False";
創(chuàng)建連接之后就可以使用DataAdapter等存取csv文件了。
詳細信息見此處
10. 如何獲得磁盤開銷信息,代碼片斷如下,主要是調(diào)用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.如何獲得不區(qū)分大小寫的子字符串的索引位置
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)
一種更優(yōu)雅的方法是使用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);
1. 如何獲得系統(tǒng)文件夾
使用System.Envioment類的GetFolderPath方法;例如:
Environment.GetFolderPath( Environment.SpecialFolder.Personal )
2. 如何獲得正在執(zhí)行的exe文件的路徑
1) 使用Application類的ExecutablePath屬性
2) System.Reflection.Assembly.GetExecutingAssembly().Location
3. 如何檢測操作系統(tǒng)的版本
使用Envioment的OSVersion屬性,例如:
OperatingSystem os = Environment.OSVersion;
MessageBox.Show(os.Version.ToString());
MessageBox.Show(os.Platform.ToString());
4. 如何根據(jù)完整的文件名獲得文件的文件名部分、
使用System.IO.Path類的方法GetFileName或者GetFileNameWithoutExtension方法
5. 如何通過文件的全名獲得文件的擴展名
使用System.IO.Path.GetExtension靜態(tài)方法
6. Vb和c#的語法有什么不同click here
7. 如何獲得當前電腦用戶名,是否聯(lián)網(wǎng),幾個顯示器,所在域,鼠標有幾個鍵等信息
使用System.Windows.Forms. SystemInformation類的靜態(tài)屬性
8. 修飾Main方法的[STAThread]特性有什么作用
標示當前程序使用單線程的方式運行
9. 如何讀取csv文件的內(nèi)容
通過OdbcConnection可以創(chuàng)建一個鏈接到csv文件的鏈接,鏈接字符串的格式是:"Driver={Microsoft Text Driver (*.txt;*.csv)};Dbq="+cvs文件的文件夾路徑+" Extensions=asc,csv,tab,txt; Persist Security Info=False";
創(chuàng)建連接之后就可以使用DataAdapter等存取csv文件了。
詳細信息見此處
10. 如何獲得磁盤開銷信息,代碼片斷如下,主要是調(diào)用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.如何獲得不區(qū)分大小寫的子字符串的索引位置
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)
一種更優(yōu)雅的方法是使用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);
相關文章
[譯]ASP.NET Core 2.0 網(wǎng)址重定向的方法
本篇文章主要介紹了[譯]ASP.NET Core 2.0 網(wǎng)址重定向的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10詳解CentOS 7.4下如何部署Asp.Net Core結合consul
這篇文章主要介紹了詳解CentOS 7.4下如何部署Asp.Net Core結合consul,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06根據(jù)身份證號碼計算出生日期、年齡、性別(18位) 根據(jù)入職時間計算工齡
根據(jù)身份證號碼計算出生日期、年齡、性別(18位);根據(jù)入職時間計算工齡實現(xiàn)代碼,需要的朋友可以參考下2012-10-10Asp.Net網(wǎng)站優(yōu)化系列之數(shù)據(jù)庫的優(yōu)化措施與索引優(yōu)化方法
索引的作用就類似于書的目錄,書的目錄會按照章節(jié)的順序排列,會指想某一張的位置。這樣如果在一本數(shù)百頁的書里面查找某個章節(jié)位置的時候,我們就可以只掃描書的目錄,掃描的范圍縮小了n倍,查詢的效率自然就提高了。2010-06-06使用Visual Studio 2017作為Linux C++開發(fā)工具
這篇文章主要為大家詳細介紹了使用Visual Studio 2017作為Linux C++開發(fā)工具的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03ASP.NET(C#)應用程序配置文件app.config/web.config的增、刪、改操作
應用程序配置文件,對于asp.net是 web.config,對于WINFORM程序是 App.Config(ExeName.exe.config)。2009-06-06ASP.NET實現(xiàn)圖書管理系統(tǒng)的步驟詳解
這篇文章主要介紹了ASP.NET圖書管理系統(tǒng)簡單實現(xiàn)步驟,本文通過實例截圖展示的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12asp.net實現(xiàn)訪問局域網(wǎng)共享目錄下文件的解決方法
這篇文章主要介紹了asp.net實現(xiàn)訪問局域網(wǎng)共享目錄下文件的解決方法,需要的朋友可以參考下2014-07-07