1 首先要使用shell32 请在项目引用中添加shell32.dll 的引用 (备注:该引用是系统dll文件 在C:\Windows\System32 目录下 可以自行拷贝到项目中)
private void btnTest_Click(object sender, EventArgs e) //测试的按钮点击事件
{
//测试,将excel中的student导入到sqlserver的DB_MES中,如果sql中的数据表不存在则创建
System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog(); // new 一个打开文件对话框用于选择文件
if (fd.ShowDialog() == DialogResult.OK)
{
string filePath = fd.FileName;
//实例化一个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);
this.richTextBox1.Text += i.ToString() + key + ":" + value + '\n'; // 窗体界面上创建的richTextBox 控件上显示所有的属性值
i++;
}
}