Skip to content

Latest commit

 

History

History
60 lines (50 loc) · 1.95 KB

how-to-retrieve-file-information-cpp-cli.md

File metadata and controls

60 lines (50 loc) · 1.95 KB
title ms.custom ms.date ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Retrieve File Information (C++/CLI) | Microsoft Docs
11/04/2016
cpp-windows
article
C++
files [C++], retrieving information about
FileInfo class
8b67f7ad-a048-4437-ac5c-b41809a6018d
9
mikeblome
mblome
ghogen

How to: Retrieve File Information (C++/CLI)

The following code example demonstrates the xref:System.IO.FileInfo class. When you have the name of a file, you can use this class to retrieve information about the file such as the file size, directory, full name, and date and time of creation and of the last modification.

This code retrieves file information for Notepad.exe.

Example

// 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;  
}  

See Also

File and Stream I-O
.NET Programming with C++/CLI (Visual C++)