Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileInfo Constructor

Initializes a new instance of the FileInfo class, which acts as a wrapper for a file path.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public FileInfo(
	string fileName


Type: System.String

The fully qualified name of the new file, or the relative file name.


fileName is null.


The caller does not have the required permission.


The file name is empty, contains only white spaces, or contains invalid characters.


Access to fileName is denied.


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.


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.

For a list of common I/O tasks, see Common I/O Tasks.

The following example uses this constructor to create two files, which are then written to, read from, copied, and deleted.

using System;
using System.IO;

class Test 
    public static void Main() 
        string path = @"c:\temp\MyTest.txt";
        FileInfo fi1 = new FileInfo(path);

        if (!fi1.Exists) 
            //Create a file to write to. 
            using (StreamWriter sw = fi1.CreateText()) 

        //Open the file to read from. 
        using (StreamReader sr = fi1.OpenText()) 
            string s = "";
            while ((s = sr.ReadLine()) != null) 

            string path2 = path + "temp";
            FileInfo fi2 = new FileInfo(path2);

            //Ensure that the target does not exist.

            //Copy the file.
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            //Delete the newly created file.
            Console.WriteLine("{0} was successfully deleted.", path2);

        catch (Exception e) 
            Console.WriteLine("The process failed: {0}", e.ToString());
//This code produces output similar to the following;  
//results may vary based on the computer/file structure/etc.: 
//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 System;
using System.IO;

public class FileInfoMainTest 
    public static void Main() 
        // Open an existing file, or create a new one.
        FileInfo fi = new 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...");
        // Get the information out of the file and display it.
        StreamReader sr = new 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.: 
//Add as many lines as you like... 
//Add another line to the output... 
//This is a new entry to add to the file 
//This is yet another line to add...

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

© 2016 Microsoft