FileInfo Constructor (String^)
Initializes a new instance of the FileInfo class, which acts as a wrapper for a file path.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- fileName
-
Type:
System::String^
The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.
| Exception | Condition |
|---|---|
| ArgumentNullException | fileName is null. |
| SecurityException | The caller does not have the required permission. |
| ArgumentException | The file name is empty, contains only white spaces, or contains invalid characters. |
| UnauthorizedAccessException | Access to fileName is denied. |
| PathTooLongException | The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. |
| NotSupportedException | fileName contains a colon (:) in the middle of the string. |
You can specify either the fully qualified or the relative file name, but the security check gets the fully qualified name.
The following example uses this constructor to create two files, which are then written to, read from, copied, and deleted.
using namespace System; using namespace System::IO; int main() { String^ path = "c:\\MyTest.txt"; FileInfo^ fi1 = gcnew FileInfo( path ); if ( !fi1->Exists ) { //Create a file to write to. StreamWriter^ sw = fi1->CreateText(); try { sw->WriteLine( "Hello" ); sw->WriteLine( "And" ); sw->WriteLine( "Welcome" ); } finally { if ( sw ) delete (IDisposable^)sw; } } //Open the file to read from. StreamReader^ sr = fi1->OpenText(); try { String^ s = ""; while ( s = sr->ReadLine() ) { Console::WriteLine( s ); } } finally { if ( sr ) delete (IDisposable^)sr; } try { String^ path2 = String::Concat( path, "temp" ); FileInfo^ fi2 = gcnew FileInfo( path2 ); //Ensure that the target does not exist. fi2->Delete(); //Copy the file. fi1->CopyTo( path2 ); Console::WriteLine( "{0} was copied to {1}.", path, path2 ); //Delete the newly created file. fi2->Delete(); Console::WriteLine( "{0} was successfully deleted.", path2 ); } catch ( Exception^ e ) { Console::WriteLine( "The process failed: {0}", e ); } } //This code produces output similar to the following; //results may vary based on the computer/file structure/etc.: // //Hello //And //Welcome //c:\MyTest.txt was copied to c:\MyTest.txttemp. //c:\MyTest.txttemp was successfully deleted.
The following example opens an existing file or creates a file, appends text to the file, and displays the results.
using namespace System; using namespace System::IO; int main() { // Open an existing file, or create a new one. FileInfo^ fi = gcnew FileInfo( "temp.txt" ); // Create a writer, ready to add entries to the file. StreamWriter^ sw = fi->AppendText(); sw->WriteLine( "This is a new entry to add to the file" ); sw->WriteLine( "This is yet another line to add..." ); sw->Flush(); sw->Close(); // Get the information out of the file and display it. StreamReader^ sr = gcnew StreamReader( fi->OpenRead() ); while ( sr->Peek() != -1 ) Console::WriteLine( sr->ReadLine() ); } //This code produces output similar to the following; //results may vary based on the computer/file structure/etc.: // //This is a new entry to add to the file //This is yet another line to add...
for reading files. Associated enumeration: FileIOPermissionAccess::Read
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0