RSS订阅
用户名: 密码:

编程技巧:.Net Framework

2007-07-02 15:35:06 作者:探索者 来源: 浏览次数:0 网友评论 0

.Net Framework

1. 如何获得系统文件夹

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

Environment.GetFolderPath( Environment. ecialFolder.Personal )

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

1) 使用A lication类的ExecutablePath属性

2) System.Reflection.A embly.GetExecutingA embly().Location

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

使用Envioment的OSVersion属性,例如:

OperatingSystem os = Environment.OSVersio 

Me ageBox.Show(os.Version.ToString());

Me ageBox.Show(os.Platform.ToString());

4. 如何根据完整的文件名获得文件的文件名部分

使用System.IO.Path类的方法GetFileName或者GetFileNameWithoutExte ion方法

5. 如何通过文件的全名获得文件的扩展名

使用System.IO.Path.GetExte ion静态方法

6. Vb和c#的语法有什么不同click here

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

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

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

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

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

通过OdbcCo ection可以创建一个链接到csv文件的链接,链接字符串的格式是:"Driver={Microsoft Text Driver (*.txt;*.csv)};Dbq="+cvs文件的文件夹路径+" Exte io =asc,csv,tab,txt; Persist Security Info=False" 

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

详细信息见此处

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

public sealed cla  DriveInfo
{
[DllImport("kernel32.dll", EntryPoint = "GetDiskFree aceExA")]
private static extern long GetDiskFree aceEx(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 GetDiskFree aceEx(drive, out available, out total, out free);
}

public static DriveInfoSystem GetInfo(string drive)
{
long result, available, total, free;
result = GetDiskFree aceEx(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.Globalizatio 

string strParent = "The Codeproject site is very informative." 

string strChild = "codeproject" 
// We create a object of CompareInfo cla  for a neutral culture or a culture i e itive object
CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;

int i = Compare.IndexOf(strParent,strChild,CompareOptio .IgnoreCase);

相关文章

[错误报告] [推荐] [收藏] [打印] [关闭] [返回顶部]

最新图片文章

最新文章


关于本站帮助(?)┊广告服务版权声明业务合作加盟我们网站地图联系我们
Copyright © 2005-2008 www.phpip.com online services. All rights reserved. 陕ICP备05002402号
在线管理员 QQ:271162318 17864997