This documentation is archived and is not being maintained.

FileInfo.DirectoryName Property

Gets a string representing the directory's full path.

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

Property Value

A string representing the directory's full path.

Exceptions

Exception Type Condition
SecurityException The caller does not have the required permission.
ArgumentNullException A null reference (Nothing in Visual Basic) was passed in for the directory name.

Remarks

To get the parent directory as a DirectoryInfo object, use the Directory property.

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
Retrieve a file extension. Path.GetExtension
Retrieve only the file name from a path. Path.GetFileNameWithoutExtension
Change the extension of a file. Path.ChangeExtension
Retrieve only the directory name from a path. Path.GetDirectoryName
Sort files in a directory by size. GetFileSystemInfos
Determine if a file exists. Exists
Determine if a directory exists. Exists

Example

The following example retrieves the full path of the specified file.

[Visual Basic] 
Dim fileName As String = "C:\autoexec.bat"
Dim fileInfo As New FileInfo(fileName)
If Not fileInfo.Exists Then
    Return
End If

Console.WriteLine("{0} has a directoryName of {1}", fileName, fileInfo.DirectoryName)

[C#] 
string   fileName = "C:\\autoexec.bat";
FileInfo fileInfo = new FileInfo(fileName);
if (!fileInfo.Exists) 
{
    return;
}

Console.WriteLine("{0} has a directoryName of {1}", 
    fileName, fileInfo.DirectoryName);

[C++] 
String*   fileName = S"C:\\autoexec.bat";
FileInfo* fileInfo = new FileInfo(fileName);
if (!fileInfo->Exists) {
    return;
}

Console::WriteLine(S" {0} has a directoryName of {1}", 
    fileName, fileInfo->DirectoryName);

[JScript] 
var fileName : String = "C:\\autoexec.bat";
var fileInfo : FileInfo = new FileInfo(fileName);
if (!fileInfo.Exists)
    return;

Console.WriteLine("{0} has a directoryName of {1}", 
                  fileName, fileInfo.DirectoryName);

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

.NET Framework Security: 

See Also

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

Show: