一些.Net编程技巧
来源:互联网 发布:汇川plc编程手册下载 编辑:程序博客网 时间:2024/06/05 08:56
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. 如何获得当前电脑用户名,是否联网,几个显示器,所在域,鼠标有几个键等信息
使用System.Windows.Forms. SystemInformation类的静态属性
7. 如何获得磁盘开销信息,代码片断如下,主要是调用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:");来获得指定磁盘的开销情况
8.如何获得不区分大小写的子字符串的索引位置
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);
- 一些.Net编程技巧
- .net编程中的一些技巧(一)
- .net编程中的一些技巧(二)
- asp.net一些技巧
- 一些的.Net技巧
- 一些的.Net技巧
- 一些编程 中的技巧
- .Net编程技巧集锦
- .Net编程技巧集锦
- .Net编程技巧集锦
- .Net 编程技巧
- .Net编程技巧集锦
- .Net编程技巧集锦
- .Net编程技巧集锦
- .NET 编程技巧点滴
- ASP.NET编程技巧
- .NET一些常用技巧整理
- asp.net一些常用技巧
- Matlab中批处理图像的方法
- 游戏数据定义是引擎的核心部分
- 用javascript刷新Iframe框架
- 金山词霸与卡巴冲突问题解决方法
- 艰苦的再学习过程~
- 一些.Net编程技巧
- MSSQL经典语句
- Common Function Objects & Function Adapters
- asp.net 实现购物车详细代码
- MAYA学习——NURBS建模2 手机
- fdsafdsa
- 在c++中qsort()排序函数的使用qsort函数应用大全
- 商业战略分析:九城关贸为何裁撤搜索引擎部门[原创]
- 想成为JAVA工程师