Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Name Property
Collapse the table of content
Expand the table of content

FileInfo.Name Property

Gets the name of the file.

[Visual Basic]
Overrides Public ReadOnly Property Name As String
[C#]
public override string Name {get;}
[C++]
public: __property String* get_Name();
[JScript]
public override function get Name() : String;

Property Value

The name of the file.

Remarks

For an example of using this property, see the Example section below. 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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending 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

Example

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

[Visual Basic] 
Imports System
Imports System.IO

Public Class NameTest
    Public Shared Sub Main()
        ' Create a reference to the current directory.
        Dim di As New DirectoryInfo(Environment.CurrentDirectory)
        ' Create an array representing the files in the current directory.
        Dim fi As FileInfo() = di.GetFiles()
        Console.WriteLine("The following files exist in the current directory:")
        ' Print out the names of the files in the current directory.
        Dim fiTemp As FileInfo
        For Each fiTemp In fi
            Console.WriteLine(fiTemp.Name)
        Next fiTemp
    End Sub 'Main
End Class 'NameTest

[C#] 
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);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int 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(S"The following files exist in the current directory:");
    // Print out the names of the files in the current directory.
    Collections::IEnumerator* myEnum = fi->GetEnumerator();
    while (myEnum->MoveNext()) {
        FileInfo* fiTemp = __try_cast<FileInfo*>(myEnum->Current);
        Console::WriteLine(fiTemp->Name);
    }
}

[JScript] 
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();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

FileInfo Class | FileInfo Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2015 Microsoft