HOW TO:擷取檔案資訊 (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;
}