gpt4 book ai didi

c# - 如何获取文件属性?

转载 作者:太空狗 更新时间:2023-10-29 18:16:44 26 4
gpt4 key购买 nike

我想要一个显示媒体文件的某些文件属性(如果可用)的应用程序,例如(不知道在 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);} ?

最佳答案

当查看或打开一个文件时,为了得到它的名字,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);

关于c# - 如何获取文件属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7861886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com