This documentation is archived and is not being maintained.

DirectoryInfo.Name Property

Gets the name of this DirectoryInfo instance.

[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 directory name.

Remarks

This Name property returns only the name of the directory, such as "Bin." To get the full path, such as "c:\public\Bin", use the FullName property.

For an example of using this method, 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
Retrieve a file extension. Path.GetExtension
Retrieve the fully qualified path of a file. Path.GetFullPath
Change the extension of a file. Path.ChangeExtension
Retrieve only the file name from a path. Path.GetFileNameWithoutExtension

Example

The following example displays the name of the current DirectoryInfo instance only.

[Visual Basic] 
Imports System
Imports System.IO

Class GetAName
    Public Shared Sub Main()
        Dim dir As New DirectoryInfo(".")
        Dim dirName As String = dir.Name
        Console.WriteLine("DirectoryInfo name is {0}.", dirName)
    End Sub

End Class

[C#] 
using System;
using System.IO;

class GetAName
{
    public static void Main(string[] args)
    {
        DirectoryInfo dir = new DirectoryInfo(".");
        String dirName=dir.Name;
        Console.WriteLine("DirectoryInfo name is {0}.", dirName);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

int main()
{
    DirectoryInfo* dir = new DirectoryInfo(S".");
    String* dirName=dir->Name;
    Console::WriteLine(S"DirectoryInfo name is {0}.", dirName);
}

[JScript] 
import System;
import System.IO;
var dir : DirectoryInfo = new DirectoryInfo(".");
var dirName : String = dir.Name;
Console.WriteLine("DirectoryInfo name is {0}.", dirName);

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

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

Show: