Path.GetDirectoryName 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
오버로드
GetDirectoryName(String) |
지정된 경로에 대한 디렉터리 정보를 반환합니다. |
GetDirectoryName(ReadOnlySpan<Char>) |
문자 범위로 표시되는 지정된 경로의 디렉터리 정보를 반환합니다. |
GetDirectoryName(String)
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
지정된 경로에 대한 디렉터리 정보를 반환합니다.
public:
static System::String ^ GetDirectoryName(System::String ^ path);
public static string GetDirectoryName (string path);
public static string? GetDirectoryName (string? path);
static member GetDirectoryName : string -> string
Public Shared Function GetDirectoryName (path As String) As String
매개 변수
- path
- String
파일 또는 디렉터리의 경로입니다.
반환
path
에 대한 디렉터리 정보이며, path
이(가) 루트 디렉터리를 나타내거나 null인 경우 null
입니다. Empty에 디렉터리 정보가 포함되어 있지 않으면 path
이(가) 반환됩니다.
예외
2.1 이전의 .NET Framework 및 .NET Core 버전: 매개 변수에 path
잘못된 문자가 포함되어 있거나, 비어 있거나, 공백만 포함됩니다.
path
매개변수가 시스템에 정의된 최대 길이보다 깁니다.
참고: Windows 스토어 앱용 .NET 또는 이식 가능한 클래스 라이브러리에서 기본 클래스 예외 를 IOException대신 catch합니다.
예제
다음 예에서는 Windows 기반 데스크톱 플랫폼에서 메서드를 사용하는 GetDirectoryName
방법을 보여 줍니다.
String^ filePath = "C:\\MyDir\\MySubDir\\myfile.ext";
String^ directoryName;
int i = 0;
while (filePath != nullptr)
{
directoryName = Path::GetDirectoryName(filePath);
Console::WriteLine("GetDirectoryName('{0}') returns '{1}'",
filePath, directoryName);
filePath = directoryName;
if (i == 1)
{
filePath = directoryName + "\\"; // this will preserve the previous path
}
i++;
}
/*
This code produces the following output:
GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir'
GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir\') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir') returns 'C:\'
GetDirectoryName('C:\') returns ''
*/
string filePath = @"C:\MyDir\MySubDir\myfile.ext";
string directoryName;
int i = 0;
while (filePath != null)
{
directoryName = Path.GetDirectoryName(filePath);
Console.WriteLine("GetDirectoryName('{0}') returns '{1}'",
filePath, directoryName);
filePath = directoryName;
if (i == 1)
{
filePath = directoryName + @"\"; // this will preserve the previous path
}
i++;
}
/*
This code produces the following output:
GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir'
GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir\') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir') returns 'C:\'
GetDirectoryName('C:\') returns ''
*/
Dim filepath As String = "C:\MyDir\MySubDir\myfile.ext"
Dim directoryName As String
Dim i As Integer = 0
While filepath <> Nothing
directoryName = Path.GetDirectoryName(filepath)
Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", _
filepath, directoryName)
filepath = directoryName
If i = 1 Then
filepath = directoryName + "\" ' this will preserve the previous path
End If
i = i + 1
End While
'This code produces the following output:
'
' GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir'
' GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir'
' GetDirectoryName('C:\MyDir\') returns 'C:\MyDir'
' GetDirectoryName('C:\MyDir') returns 'C:\'
' GetDirectoryName('C:\') returns ''
설명
대부분의 경우 이 메서드에서 반환된 문자열은 마지막 디렉터리 구분 문자는 포함하지 않지만 경로의 모든 문자로 구성됩니다. 디렉터리 구분 기호 문자는 또는 AltDirectorySeparatorChar일 DirectorySeparatorChar 수 있습니다. 경로가 루트 디렉터리(예: "c:\" null
)로 구성된 경우 가 반환됩니다.
이 메서드는 "file:"을 사용하는 경로를 지원하지 않습니다.
반환된 경로에는 마지막 디렉터리 구분 문자가 포함되지 않으므로 반환된 경로를 메서드로 GetDirectoryName 다시 전달하면 결과 경로에서 후속 호출당 하나의 폴더 수준이 잘립니다. 예를 들어 경로 "C:\Directory\SubDirectory\test.txt"을 로 GetDirectoryName 전달하면 "C:\Directory\SubDirectory"가 반환됩니다. 해당 경로 "C:\Directory\SubDirectory"를 "C:\Directory"로 GetDirectoryName 전달합니다.
일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.
추가 정보
적용 대상
GetDirectoryName(ReadOnlySpan<Char>)
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
문자 범위로 표시되는 지정된 경로의 디렉터리 정보를 반환합니다.
public:
static ReadOnlySpan<char> GetDirectoryName(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetDirectoryName (ReadOnlySpan<char> path);
static member GetDirectoryName : ReadOnlySpan<char> -> ReadOnlySpan<char>
Public Shared Function GetDirectoryName (path As ReadOnlySpan(Of Char)) As ReadOnlySpan(Of Char)
매개 변수
- path
- ReadOnlySpan<Char>
디렉터리 정보를 검색할 경로입니다.
반환
에 대한 path
디렉터리 정보 또는 가 , 빈 범위 또는 루트(예: \, C:또는 \\server\share)인 경우 path
빈 범위입니다 null
.
설명
문자열 오버로드와 달리 이 메서드는 디렉터리 구분 기호를 정규화하지 않습니다.
추가 정보
적용 대상
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기