如何获取文件属性?
我想它显示一个媒体文件的一些文件属性的应用程序(如果可用),喜欢(不知道在Windows中使用它确切的英文单词)的文件名,距离/时间,文件类型(.AVI .MP3等) 我试着标签库和windowsapishell,但我没有得到一个工作结果(参考文献都不错)如何获取文件属性?
ShellFile so = ShellFile.FromFilePath(file);
so.Properties.System.(everythingIwant)
显示了我很多,我希望显示的文件属性的,但我不能让它工作 错误的一个例子:
'WindowsFormsApplication2.vshost.exe'(Managed(v4.0.30319)):Loaded'C:\ Windows \ Microsoft.Net \ assembly \ GAC_MSIL \ WindowsBase \ v4.0 _4.0.0.0__31bf3856ad364e35 \ WindowsBase.dll中”,跳过加载符号。模块已经过优化,调试器选项“Just My Code”已启用。 程序 '[6300] WindowsFormsApplication2.vshost.exe:程序跟踪' 已退出,代码0(为0x0)。 程序 '[6300] WindowsFormsApplication2.vshost.exe:托管(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.)
但我怎么使用这个功能,我想不一样,如果(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);
这正是我一直在寻找的! ;)非常感谢(所有) – ComputerIntelligentAgent
欢迎您,如果这些信息真的对您有所帮助不要忘记标记为答案 –
@ user1008817我已编辑我的答案,请看看我的答案... –
这正是我一直在寻找的! ;)非常感谢(所有) – ComputerIntelligentAgent
@ user1008817,如果这个答案实际上回答了你的问题,你应该接受它(使用绿色的勾号)。你还应该提供所有你认为有帮助的答案。 – svick
该程序似乎成功运行。您提供的消息不是错误消息,只是通常的诊断。你忘了输出找到的值吗?或者,也许你只需要在'Main'的最后一行设置一个断点? – Vlad
.net [System.FileInfo](http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx)类可能会提供您需要的许多结果。它不会提供诸如媒体文件的持续时间等信息,但您可能会从这里开始寻找更基本的信息,以此作为实践应用程序的跳板。 –
http://thecodeisart.blogspot.com/2008/11/file-attributes-with-c.html如果你想获得音频文件的长度然后看看这里http://social.msdn.microsoft.com/forums/zh-CN/Vsexpressvcs/thread/b83106f2-e16d-4f30-ac31-854055c9efdc /#2685871 –