如何:检索文件信息 (C++/CLI)
下面的代码示例演示 FileInfo 类。 获取文件名称后,就可以使用此类检索文件信息,例如文件大小、目录、全名、创建日期和时间,以及上次修改的日期和时间。
此代码检索 Notepad.exe 的文件信息。
示例
// file_info.cpp
// compile with: /clr
using namespace System;
using namespace System::IO;
int main()
{
array<String^>^ args = Environment::GetCommandLineArgs();
if (args->Length < 2)
{
Console::WriteLine("\nUSAGE : file_info <filename>\n\n");
return -1;
}
FileInfo^ fi = gcnew FileInfo( args[1] );
Console::WriteLine("file size: {0}", fi->Length );
Console::Write("File creation date: ");
Console::Write(fi->CreationTime.Month.ToString());
Console::Write(".{0}", fi->CreationTime.Day.ToString());
Console::WriteLine(".{0}", fi->CreationTime.Year.ToString());
Console::Write("Last access date: ");
Console::Write(fi->LastAccessTime.Month.ToString());
Console::Write(".{0}", fi->LastAccessTime.Day.ToString());
Console::WriteLine(".{0}", fi->LastAccessTime.Year.ToString());
return 0;
}