欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用shell32获取文件属性的方法

程序员文章站 2022-10-25 19:02:54
本文实例讲述了c#使用shell32获取文件属性的方法。分享给大家供大家参考。具体实现方法如下: using system; using system.col...

本文实例讲述了c#使用shell32获取文件属性的方法。分享给大家供大家参考。具体实现方法如下:

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using shell32; 
namespace getfilecreator 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   //要获取属性的文件路径 
   string filepath = @"e:/f/aa.txt"; 
   //初始化shell接口 
   shell32.shell shell = new shell32.shellclass(); 
   //获取文件所在父目录对象 
   folder folder = shell.namespace(filepath.substring(0, filepath.lastindexof('//'))); 
   //获取文件对应的folderitem对象 
   folderitem item = folder.parsename(filepath.substring(filepath.lastindexof('//')+1)); 
   //字典存放属性名和属性值的键值关系对 
   dictionary<string, string> properties = new dictionary<string, string>(); 
   int i =0; 
   while (true) 
   { 
    //获取属性名称 
    string key = folder.getdetailsof(null, i); 
    if (string.isnullorempty(key)) 
    { 
     //当无属性可取时,推出循环 
     break; 
    } 
    //获取属性值 
    string value = folder.getdetailsof(item, i); 
    //保存属性 
    properties.add(key, value); 
    i++; 
   } 
  } 
 } 
}

希望本文所述对大家的c#程序设计有所帮助。