C#中获取文件属性

C#中获取文件属性

2023年7月6日发(作者:)

C#中获取⽂件属性在Explorer中,选择“详细”视图,可以看到很多⽂件属性,如图⽚的分辨率,MP3的艺术家、⽐特率,视频⽂件的分辨率等等等等;这些数据,有时候在C#中可能很难获取,⽐如图⽚的分辨率,需要⽤le得到Image类,然后才能得到分辨率数据,但是把图⽚加载到内存中,⼀是会加⼤内存开销,⼆是会更耗时。所以,今天我们就来说说怎么在C#中获取到⽂件的详细属性。添加引⽤在你的项⽬中添加对C:的引⽤,我们在前⾯⼀期《C#中使⽤SHFileOperation调⽤Windows的复制⽂件对话框》中也⽤到了这个动态链接库,不过当时是使⽤映射的⽅式,如今我们将其引⽤,直接使⽤。using Shell32;代码实现 ///

/// 获取⽂件属性字典 /// /// ⽂件路径 /// 属性字典 public static Dictionary GetProperties(string filePath) { if (!(filePath)) { throw new FileNotFoundException("指定的⽂件不存在。", filePath); } //初始化Shell接⼝

shell = new lass(); //获取⽂件所在⽗⽬录对象

Folder folder = ace(ectoryName(filePath)); //获取⽂件对应的FolderItem对象

FolderItem item = ame(eName(filePath)); //字典存放属性名和属性值的键值关系对

Dictionary Properties = new Dictionary(); int i = 0; while (true) { //获取属性名称

string key = ailsOf(null, i); if (OrEmpty(key)) { //当⽆属性可取时,退出循环

break; } //获取属性值

string value = ailsOf(item, i); //保存属性 (key, value); i++; } return Properties; }GetProperties这个⽅法返回所有属性值,在我的Win7 Pro 64bit 上,返回了287个属性!可以想象,信息是很丰富的,但是速度也是够慢的。可以看到,上⾯代码⽤了⼀个循环,获取属性名和属性值时都是通过i来索引的。那么,我们是不是就能不通过循环,⽽直接⽤下标来获取想要的属性呢?代码如下: ///

/// 获取指定⽂件指定下标的属性值 /// /// ⽂件路径 /// 属性下标 /// 属性值 public static string GetPropertyByIndex(string filePath, int index) { if (!(filePath)) { throw new FileNotFoundException("指定的⽂件不存在。", filePath); } //初始化Shell接⼝

shell = new lass(); //获取⽂件所在⽗⽬录对象

Folder folder = ace(ectoryName(filePath)); //获取⽂件对应的FolderItem对象

FolderItem item = ame(eName(filePath)); string value = null;

//获取属性名称

string key = ailsOf(null, index); if (false == OrEmpty(key)) { //获取属性值

value = ailsOf(item, index); } return value; }GetPropertyByIndex在我的系统环境上,分辨率“尺⼨”下标是31,那么我只需要GetPropertyByIndex(@“D:”,31)就可以获取到分辨率信息了。但是特别需要注意,“尺⼨”属性的下标,在不同的Windows版本(XP,Vista,Win7,Win2003等)不⼀定是⼀样的。ok,我们还注意到每个属性都有对应的⼀个“属性名”,那么,我们能不能通过属性名来获取属性值呢,这样会⽐使⽤下标保险多了吧。代码如下: ///

/// 获取指定⽂件指定属性名的值 /// /// ⽂件路径 /// 属性名 /// 属性值 public static string GetProperty(string filePath, string propertyName) { if (!(filePath)) { throw new FileNotFoundException("指定的⽂件不存在。", filePath); } //初始化Shell接⼝

shell = new lass(); //获取⽂件所在⽗⽬录对象

Folder folder = ace(ectoryName(filePath)); //获取⽂件对应的FolderItem对象

FolderItem item = ame(eName(filePath)); string value = null; int i = 0; while (true) { //获取属性名称

string key = ailsOf(null, i); if (OrEmpty(key)) { //当⽆属性可取时,退出循环

break; } if (true == (key, propertyName, tCultureIgnoreCase)) { //获取属性值

value = ailsOf(item, i); break; }

i++; } return value; }GetProperty这个⽅法是我⼀开始写的,通过在while⾥⾯加上属性名的判断,直到找到对应的属性名,则返回相应的属性值。不过这个⽅法还是不够简洁,“尺⼨”属性在31,意味着每⼀次都需要循环31次才能拿到我要的值,如果我要获取的属性名下标为287(参看上⾯),那么次数将更多,于是,我⼜对代码做了⼀些优化: ///

/// 存储属性名与其下标(key值均为⼩写) /// private static Dictionary _propertyIndex = null;

///

/// 获取指定⽂件指定属性名的值 /// /// ⽂件路径 /// 属性名 /// 属性值 public static string GetPropertyEx(string filePath, string propertyName) { if (_propertyIndex == null) { InitPropertyIndex(); } //转换为⼩写 string propertyNameLow = r(); if (_nsKey(propertyNameLow)) { int index = _propertyIndex[propertyNameLow]; return GetPropertyByIndex(filePath, index); } return null; }

///

/// 初始化属性名的下标 /// private static void InitPropertyIndex() { Dictionary propertyIndex = new Dictionary(); //获取本代码所在的⽂件作为临时⽂件,⽤于获取属性列表 string tempFile = cutingAssembly().FullName; Dictionary allProperty = GetProperties(tempFile); if (allProperty != null) { int index = 0; foreach (var item in ) { //属性名统⼀转换为⼩写,⽤于忽略⼤⼩写 _(r(), index); index++; } } _propertyIndex = propertyIndex; }GetPropertyEx_propertyIndex⽤于存储属性名与其下标,⽤Dictionary是因为_propertyIndex[key]的时间复杂度是O(1)。然后在GetPropertyEx⽅法中找到属性名对应的下标,直接返回该下标的属性值。InitPropertyIndex⽅法只会被调⽤⼀次。好了,我们现在通过属性名来获取属性值,在不同系统之间应该不会有问题了吧?不⼀定,原因你肯定也想到了,如果是在⼀个英⽂windows上,它的属性名⾥⾯不会有“尺⼨”,对应的应该是“Resolution”之类的(我没有英⽂版系统,所以只是猜测),也不会有“名称”属性,⽽是“Name”;总结⼀下,⽅法名GetPropertyByIndexGetPropertyEx适⽤不同语⾔的系统不同版本的系统不适⽤不同版本的系统不同语⾔的系统所以,根据你的程序可能的运⾏环境,选择适合你的⽅法;再思考:要能在不同语⾔不同版本的系统将通⽤,该怎么办?我⽬前想到的:建⽴同⼀个属性名在不同语⾔间的对应关系,如”尺⼨”对应” Resolution”,然后,在代码⾥获取到系统语⾔,将属性名“翻译”成该语⾔,即可通过翻译后的属性名找到对应的属性值了。欢迎有实现了本⽅法或者更好⽅法的同学⼀起来讨论。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1688598270a153669.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信