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 */
public String get_Name ()

public override function get Name () : String

Property Value

The name of the file.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText

Retrieve a file extension.

Path.GetExtension

Retrieve the fully qualified path of a file.

Path.GetFullPath

Retrieve the file name and extension from a path.

Path.GetFileName

Change the extension of a file.

Path.ChangeExtension

Retrieve only the directory name from a path.

Path.GetDirectoryName

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);
    }
}

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

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();

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft