获取特定的文件属性 [英] Getting specific file attributes

查看:166
本文介绍了获取特定的文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WCF服务,让客户/消费者上传图片,音频或视频文件到它。上传后,该服务是应该分​​析该文件并以某种方式检索以下属性:



图像:宽度,高度,拍摄日期,使用的程序



音频:运行时,艺术家,专辑,流派,比特率,出版年份



视频:运行时,宽度,高度,帧/秒,视频比特率,音频比特率



显然,Windows可以获取并很容易地显示这些属性,但我怎么做它在C#?


< DIV CLASS =h2_lin>解决方案

的Courtesty这个线程。



我已经验证了这一点得到所有文件属性包括扩展属性。



在你的项目去'添加引用' - > COM - 说线程>微软壳牌控制和自动化



补充一点,再次礼貌,一个C#方法来读取文件的目录中的属性。 (我还在研究,看看是否有可能在一个特定的文件执行此功能。如果没有,你总是可以传递文件名有问题,并确认只获得属性了该文件。)

 公共静态无效的主要(字串[] args)
{
名单,LT;字符串> arrHeaders =新的List<串GT;();

Shell32.Shell壳=新Shell32.Shell();
Shell32.Folder objFolder;

objFolder = shell.NameSpace(@C:\temp\testprop);

的for(int i = 0; I< short.MaxValue;我++)
{
串头= objFolder.GetDetailsOf(NULL,I);
如果(String.IsNullOrEmpty(头))
中断;
arrHeaders.Add(标题);
}

的foreach
{
(在objFolder.Items()Shell32.FolderItem2项目)的for(int i = 0; I< arrHeaders.Count;我++ )
{
Console.WriteLine({0} \t {1}:{2},我,arrHeaders [I],objFolder.GetDetailsOf(项目一));
}
}
}


I've got a simple WCF service that lets clients/consumers upload image, audio or video files to it. After the upload, the service is supposed to analyze the file and somehow retrieve the following attributes:

Image: width, height, date taken, program used

Audio: runtime, artist, album, genre, bitrate, publication year

Video: runtime, width, height, frames/sec, video bitrate, audio bitrate

Apparently Windows can get and display these attributes pretty easily, but how do I do it in C#?

解决方案

Courtesty of this thread.

I've verified this gets all file attributes including the extended attributes.

In your project go to 'Add Reference' -> COM -> 'Microsoft Shell Controls and Automation'

Add that, and again courtesy of said thread, a C# method to read the attributes of the files in a directory. (I'm still researching to see if it's possible to perform this functionality on a specific file. If not you could always pass the filename in question and verify to only get the attributes out for that file.)

public static void Main(string[] args)
{
    List<string> arrHeaders = new List<string>();

    Shell32.Shell shell = new Shell32.Shell();
    Shell32.Folder objFolder;

    objFolder = shell.NameSpace(@"C:\temp\testprop");

    for( int i = 0; i < short.MaxValue; i++ )
    {
        string header = objFolder.GetDetailsOf(null, i);
        if (String.IsNullOrEmpty(header))
            break;
        arrHeaders.Add(header);
    }

    foreach(Shell32.FolderItem2 item in objFolder.Items())
    {
        for (int i = 0; i < arrHeaders.Count; i++)
        {
            Console.WriteLine("{0}\t{1}: {2}", i, arrHeaders[i], objFolder.GetDetailsOf(item, i));
        }
    }
}

这篇关于获取特定的文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆