在上一篇《浏览.net framework 2.0 类型库中新增的常用功能》一文中我主要列了几个新增的常用主件,本文作为小结主要针对一些常用类的扩展来讲最近在使用c# 2.0 的时候发现的几个新特征,讲得不当之处请网友指正。
1.exception异常基类
在2.0下,exception基类增加了data属性,原型如下,
public virtual idictionary data {get;}
可见其实现了idictionary接口,用来存储异常的自定义信息,由此想到在exceptionmanagement block中通过继承增加namevaluecollection类成员来使baseapplicationexception具有该项功能,exception新增data属性的灵感来源于此?
2.file增加解密加密功能
使用file的新增加密解密方法来保护文件。在windows2003系统窗口的文件夹选项菜单的查看选项卡中选中用彩色显示加密或压缩的ntfs文件复选框(在xp或2000系统里应该也有相关的选项)就可以看到被加密的文件颜色会不一样。
具体方法定义如下,
public static void encrypt( string path ); //加密
public static void decrypt( string path );//解密
加密后,文件就会变成绿色,如果该文件没有授权给其他用户,那在其他用户登录时就无法访问该文件。点击加密文件属性可以得到加密的更多信息。
3.driveinfo类
driveinfo类提供系统驱动器的信息,是.net 2.0下新增的类,可以通过
driveinfo[] drivers = driveinfo.getdrives();
得到驱动信息,如:
availablefreespace |
indicates the amount of available free space on a drive.(配额考虑在内) |
driveformat |
gets the name of the file system, such as ntfs or fat32. |
drivetype |
gets the drive type. |
isready |
gets information on whether or not the drive is ready. |
name |
gets the name of the drive. |
rootdirectory |
gets the root directory of the drive. |
totalfreespace |
gets the total amount of free space available on a drive. |
totalsize |
gets the total size of storage space on the drive. |
volumelabel |
gets and sets the volume label of the drive. |
上面的volumelabel是可读写的,其他属性是只读的。在使用时一般需先判断isready属性是否为true,如果没有准备好,那访问其他属性就会发生异常,还有需要注意在编程时是否有权限访问。
drivetype枚举也是在.net 2.0下新增的,
member name |
description |
cdrom |
the drive is a cd rom device. |
fixed |
the drive is a fixed disk.(固定磁盘驱动器) |
network |
the drive is a network drive.(网络驱动器) |
norootdirectory |
the drive does not have a root directory.(不含根目录的驱动器) |
ram |
the drive is a ram disk.(ram闪存) |
removable |
the drive is a removable storage device.(可移动存储设备) |
unknown |
the type of drive is unknown.(未知设备类型) |
在1.1版中使用directory.getlogicaldrives();来得到驱动器。当然使用wmi也可以实现上述所有功能。
4.system.windows.forms.menu类
在2.0中增加了tag属性,这样从它继承的menuitem也就包含了该属性,就像treenode.tag属性可以保存各种对象。
5.console类明显得到增强
consle增加了很多功能,包括设置控制台窗体的外观大小和颜色,还可以设置、移动里面的光标,设置缓冲区,判断键盘的那些特定键是否开启等等。举个例子像console.readkey ()以及它的重载方法将会很有用。
以上特征是我针对.net framework 2.0 beta 2来写的,在正式版出来后也许会有些改动。
