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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft