编程技巧:.NetFramework

来源:互联网 发布:java 图片合成工具 编辑:程序博客网 时间:2024/05/16 05:25
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
.Kqd767{display:none;}

.NetFramework

1.如何获得系统文件夹

使用System.Envioment类的GetFolderPath方法;例如:

Environment.GetFolderPath(Environment.SpecialFolder.Personal)

2.如何获得正在执行的exe文件的路径

1)使用Application类的ExecutablePath属性

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

3.如何检测操作系统的版本

使用Envioment的OSVersion属性,例如:

OperatingSystemos=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#的语法有什么不同clickhere

7.如何获得当前电脑用户名,是否联网,几个显示器,所在域,鼠标有几个键等信息

使用System.Windows.Forms.SystemInformation类的静态属性

8.修饰Main方法的[STAThread]特性有什么作用

标示当前程序使用单线程的方式运行

9.如何读取csv文件的内容

通过OdbcConnection可以创建一个链接到csv文件的链接,链接字符串的格式是:"Driver={MicrosoftTextDriver(*.txt;*.csv)};Dbq="+cvs文件的文件夹路径+"Extensions=asc,csv,tab,txt;PersistSecurityInfo=False";

创建连接之后就可以使用DataAdapter等存取csv文件了。

详细信息见此处

10.如何获得磁盘开销信息,代码片断如下,主要是调用kernel32.dll中的GetDiskFreeSpaceEx外部方法。

publicsealedclassDriveInfo
{
[DllImport("kernel32.dll",EntryPoint="GetDiskFreeSpaceExA")]
privatestaticexternlongGetDiskFreeSpaceEx(stringlpDirectoryName,
outlonglpFreeBytesAvailableToCaller,
outlonglpTotalNumberOfBytes,
outlonglpTotalNumberOfFreeBytes);

publicstaticlongGetInfo(stringdrive,outlongavailable,outlongtotal,outlongfree)
{
returnGetDiskFreeSpaceEx(drive,outavailable,outtotal,outfree);
}

publicstaticDriveInfoSystemGetInfo(stringdrive)
{
longresult,available,total,free;
result=GetDiskFreeSpaceEx(drive,outavailable,outtotal,outfree);
returnnewDriveInfoSystem(drive,result,available,total,free);
}
}

publicstructDriveInfoSystem
{
publicreadonlystringDrive;
publicreadonlylongResult;
publicreadonlylongAvailable;
publicreadonlylongTotal;
publicreadonlylongFree;

publicDriveInfoSystem(stringdrive,longresult,longavailable,longtotal,longfree)
{
this.Drive=drive;
this.Result=result;
this.Available=available;
this.Total=total;
this.Free=free;
}
}

可以通过DriveInfoSysteminfo=DriveInfo.GetInfo("c:");来获得指定磁盘的开销情况

11.如何获得不区分大小写的子字符串的索引位置

1)通过将两个字符串转换成小写之后使用字符串的IndexOf方法:

stringstrParent="TheCodeprojectsiteisveryinformative.";

stringstrChild="codeproject";

//Thelinebelowwillreturn-1whenexpectedis4.
inti=strParent.IndexOf(strChild);

//Thelinebelowwillreturnproperindex
intj=strParent.ToLower().IndexOf(strChild.ToLower());

2)一种更优雅的方法是使用System.Globalization命名空间下面的CompareInfo类的IndexOf方法:

usingSystem.Globalization;

stringstrParent="TheCodeprojectsiteisveryinformative.";

stringstrChild="codeproject";
//WecreateaobjectofCompareInfoclassforaneutralcultureoracultureinsensitiveobject
CompareInfoCompare=CultureInfo.InvariantCulture.CompareInfo;

inti=Compare.IndexOf(strParent,strChild,CompareOptions.IgnoreCase);

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击