10

我想要一个显示媒体文件的一些文件属性(如果可用)的应用程序,比如(不知道 Windows 中使用的确切英文单词)文件名、长度/持续时间、文件类型(.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',跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。程序“[6300] WindowsFormsApplication2.vshost.exe:程序跟踪”已退出,代码为 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);} ?

4

2 回答 2

13

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

FileInfo oFileInfo = new FileInfo(strFilename);

if (oFileInfo != null || oFileInfo.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);
于 2011-10-22T19:02:47.323 回答
6

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

除此之外,您可能希望使用FileInfo 类。

你还在看什么其他类型的房产?

于 2011-10-22T19:07:24.693 回答