2014-09-15 1 views
0

에서 파일 이름을 얻을 수 있습니다 :어떻게이 문자열에서 파일 이름을 얻을 수있는 방법이 문자열

"C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg" 

예를 들어, 내 결과는 다음과 같아야합니다 : "Lighthouse.jpg".

using System.IO; 
// ... 
var filePath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg"; 
var fileName = Path.GetFileName(filePath); 
+6

구글이 아래로 오늘 또는 뭔가 :

 var filePath = @"E:\raviiii\fibo.txt"; var fileName = Path.GetFileName(filePath); Console.WriteLine(fileName); Console.ReadLine(); 
John3136

+0

[문자열에서 파일 이름 가져 오기] 가능한 중복 (http://stackoverflow.com/questions/2742688/getting-file-name-from-the-string) –

답변

2

봅니다

stirng filePath = @"C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg"; 

string fileName = System.IO.Path.GetFileName(filePath); 

또는

string fileName = filePath.Substring(filePath.LastIndexOf("\\")+1); 
6

당신은 Path.GetFileName(msdn) 사용해야 사용할 수 있습니다.

string fileName = @"C:\mydir\myfile.ext"; 
string path = @"C:\mydir\"; 
string result; 

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

당신이 Path.GetFileName를 사용할 수 있습니다

0

는 아래의 코드 샘플을 사용?
관련 문제