Export (0) Print
Expand All

FileInfo.Name Property

Gets the name of the file.

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

public override string Name { get; }

Property Value

Type: System.String
The name of the file.

When first called, FileInfo calls Refresh and caches information about the file. On subsequent calls, you must call Refresh to get the latest copy of the information.

The name of the file includes the file extension.

The following example uses the Name property to display the names of files in the current directory.

using System;
using System.IO;

public class NameTest 
{
    public static void Main() 
    {
        // Create a reference to the current directory.
        DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
        // Create an array representing the files in the current directory.
        FileInfo[] fi = di.GetFiles();
        Console.WriteLine("The following files exist in the current directory:");
        // Print out the names of the files in the current directory. 
        foreach (FileInfo fiTemp in fi)
            Console.WriteLine(fiTemp.Name);
    }
}
//This code produces output similar to the following;  
//results may vary based on the computer/file structure/etc.: 
// 
//The following files exist in the current directory: 
//fileinfoname.exe 
//fileinfoname.pdb 
//newTemp.txt

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft