如何获取文件属性?

我想要一个显示媒体文件的一些文件属性的应用程序,如果可用,比如(不知道windows中使用的确切英文单词)FileName,Length / Duration,FileType(.avi .mp3等)我试过taglib和windowsapishell但我没有得到一个工作结果(参考是好的)

ShellFile so = ShellFile.FromFilePath(file); so.Properties.System.(everythingIwant) 

向我展示了我想要显示的很多文件属性,但我无法让它工作一个错误的例子:

‘WindowsFormsApplication2.vshost.exe’(托管(v4.0.30319)):已加载’C:\ Windows \ Microsoft.Net \ assembly \ GAC_MSIL \ WindowsBase \ v4.0_4.0.0.0__31bf3856ad364e35 \ WindowsBase.dll’,跳过加载符号。 模块已经过优化,调试器选项“Just My Code”已启用。 程序'[6300] WindowsFormsApplication2.vshost.exe:Program Trace’已退出,代码为0(0x0)。 程序'[6300] WindowsFormsApplication2.vshost.exe:Managed(v4.0.30319)’已退出,代码为0(0x0)。

容易的事情

 var thing = so.Properties.System.FileName.Description; Console.WriteLine(thing); 

不会工作

我知道一些Java和PHP编程,但我对C#完全不熟悉


特别感谢@ marr75和@errorstacks!

一个跟进问题:我做了这个,它有效

 class Program { static void Main(string[] args) { string file = "E:/Dump/Shutter Island.avi"; FileInfo oFileInfo = new FileInfo(file); Console.WriteLine("My File's Name: \"" + oFileInfo.Name + "\""); DateTime dtCreationTime = oFileInfo.CreationTime; Console.WriteLine("Date and Time File Created: " + dtCreationTime.ToString()); Console.WriteLine("myFile Extension: " + oFileInfo.Extension); Console.WriteLine("myFile total Size: " + oFileInfo.Length.ToString()); Console.WriteLine("myFile filepath: " + oFileInfo.DirectoryName); Console.WriteLine("My File's Full Name: \"" + oFileInfo.FullName + "\""); } } 

但是如果信息存在,我希望它只向我提供信息。 我看到了

  **Exists** Gets a value indicating whether a file exists. (Overrides FileSystemInfo.Exists.) 

但是我怎么使用这个函数,我想不喜欢if(io.ofileinfo.FullName.exist){Console.Write(io.ofileinfo.fullname);}?

你可以尝试这样….使用c#

在查看或打开文件时,要获取其名称,FileInfo类将配备Name属性。 这是一个示例代码:

 FileInfo oFileInfo = new FileInfo(strFilename); if (FileName != null || FileName.Length == 0) { MessageBox.Show("My File's Name: \"" + oFileInfo.Name + "\""); // For calculating the size of files it holds. MessageBox.Show("myFile total Size: " + oFileInfo.Length.ToString()); } 

你可以这样检查

 if (!oFileInfo.Exists) { throw new FileNotFoundException("The file was not found.", FileName); } 

要查找这些日期和时间值是什么,可以使用“访问文件系统信息”属性。

 DateTime dtCreationTime = oFileInfo.CreationTime; MessageBox.Show("Date and Time File Created: " + dtCreationTime.ToString()); 

要了解文件的扩展名,可以访问FileSystemInfo.Extension属性的值。

 MessageBox.Show("myFile Extension: " + oFileInfo.Extension); 

这是一个链接,其中包含有关查看属性的信息。

除此之外, FileInfo类是你可能想要使用的。

您还在寻找其他什么类型的房产?