DirectoryInfo.Name Property

Definition

Gets the name of this DirectoryInfo instance.

C#
public override string Name { get; }

Property Value

The directory name.

Examples

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

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

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.

The Name property of a DirectoryInfo requires no permission (beyond the read permission to the directory necessary to construct the Exists) but can give out the directory name. If it is necessary to hand out a DirectoryInfo to a protected directory with a cryptographically secure name, create a dummy directory for the untrusted code's use.

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

Applies to

Produkt Versjoner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also