다음을 통해 공유


Path.GetFileName 메서드

지정된 경로 문자열에서 파일 이름과 확장명을 반환합니다.

네임스페이스: System.IO
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Shared Function GetFileName ( _
    path As String _
) As String
‘사용 방법
Dim path As String
Dim returnValue As String

returnValue = Path.GetFileName(path)
public static string GetFileName (
    string path
)
public:
static String^ GetFileName (
    String^ path
)
public static String GetFileName (
    String path
)
public static function GetFileName (
    path : String
) : String

매개 변수

  • path
    파일 이름과 확장명을 가져올 경로 문자열입니다.

반환 값

path에서 마지막 디렉터리 문자 다음의 문자들이 들어 있는 String입니다. path의 마지막 문자가 디렉터리나 볼륨 구분 문자이면 이 메서드는 Empty를 반환합니다. path이 Null 참조(Visual Basic의 경우 Nothing)이면 이 메서드는 Null 참조(Visual Basic의 경우 Nothing)을 반환합니다.

예외

예외 형식 조건

ArgumentException

path에 InvalidPathChars에 정의된 잘못된 문자가 하나 이상 포함되어 있거나 와일드카드 문자가 포함되어 있는 경우

설명

파일 경로가 Null 참조(Visual Basic의 경우 Nothing)이면 반환 값은 Null 참조(Visual Basic의 경우 Nothing)입니다.

파일 이름 시작 부분을 확인하는 데 사용되는 구분 문자는 DirectorySeparatorCharAltDirectorySeparatorChar입니다.

이 메서드를 사용하는 방법에 대한 예제를 보려면 아래 예제 단원을 참조하십시오. 다음 표에서는 일반적인 예 또는 관련된 I/O 작업의 예를 보여 줍니다.

수행 작업

참조 항목

텍스트 파일을 만듭니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에 씁니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에서 읽습니다.

방법: 파일의 텍스트 읽기

파일의 정규화된 경로를 검색합니다.

GetFullPath

경로에서 파일 이름만 검색합니다.

GetFileNameWithoutExtension

경로에서 디렉터리 이름만 검색합니다.

GetDirectoryName

확장명을 변경합니다.

ChangeExtension

예제

다음 코드 예제에서는 Windows 기반 데스크톱 플랫폼에서의 GetFileName 메서드 동작을 보여 줍니다.

Dim fileName As String = "C:\mydir\myfile.ext"
Dim pathname As String = "C:\mydir\"
Dim result As String

result = Path.GetFileName(fileName)
Console.WriteLine("GetFileName('{0}') returns '{1}'", fileName, result)

result = Path.GetFileName(pathname)
Console.WriteLine("GetFileName('{0}') returns '{1}'", pathname, result)

' This code produces output similar to the following:
'
' GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
' GetFileName('C:\mydir\') returns ''
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;

result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
    fileName, result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
    path, result);

// This code produces output similar to the following:
//
// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''
String^ fileName = "C:\\mydir\\myfile.ext";
String^ path = "C:\\mydir\\";
String^ result;
result = Path::GetFileName( fileName );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", fileName, result );
result = Path::GetFileName( path );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", path, result );
String fileName = "C:\\mydir\\myfile.ext";
String path = "C:\\mydir\\";
String result;

result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'",fileName,result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", path, result);
var fileName : String = "C:\\mydir\\myfile.ext";
var path : String = "C:\\mydir\\";
var result : String;

result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
                  fileName, result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
                  path, result);

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Path 클래스
Path 멤버
System.IO 네임스페이스

기타 리소스

파일 및 스트림 I/O
방법: 파일의 텍스트 읽기
방법: 파일에 텍스트 쓰기