Information
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.Name Property

Gets the name of the file.

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

public override string Name { get; }
/** @property */
public String get_Name ()

public override function get Name () : String

Not applicable.

Property Value

The name of the file.

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

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

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

import System.*;
import System.IO.*;

public class NameTest
{
    public static void main(String[] args)
    {
        // Create a reference to the current directory.
        DirectoryInfo di = new DirectoryInfo(Environment.
            get_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.
        for (int iCtr = 0; iCtr < fi.length; iCtr++) {
            FileInfo fiTemp = fi[iCtr];
            Console.WriteLine(fiTemp.get_Name());
        }
    } //main
} //NameTest

//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.jsl

import System;
import System.IO;

public class NameTest {
    public static function Main() : void {

        // Create a reference to the current directory.
        var di : DirectoryInfo = new DirectoryInfo(Environment.CurrentDirectory);

        // Create an array representing the files in the current directory.
        var fi : FileInfo[] = di.GetFiles();

        Console.WriteLine("The following files exist in the current directory:");

        // Print out the names of the files in the current directory.
        for (var i : int in fi)
            Console.WriteLine(fi[i]);
    }
}
NameTest.Main();

//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.js

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2015 Microsoft