2012-10-22 3 views
22

그래서, 나는경로에서 파일 이름과 확장명을 추출하는 방법?

string path = "C:\\Program Files\\Program\\File.exe"; 

하는 방법은 "FILE.EXE"어떻게해야합니까이 있다고? 나눠 봤지만 (아래 참조) 무언가를 생각하고 있었지만 시도한 것은 작동하지 않습니다 ...

이것은 내 코드입니다.

 List<string> procs = new List<string>(); //Used to check if designated process is already running 
     foreach (Process prcs in Process.GetProcesses()) 
      procs.Add(prcs.ProcessName); //Add each process to the list 
     foreach (string l in File.ReadAllLines("MultiStart.txt")) //Get list of processes (full path) 
      if (!l.StartsWith("//")) //Check if it's commented out 
       if (!procs.Contains(l.Split('\\')[l.Split('\\').Length - 1])) //Check if process is already running 
        Process.Start(l); 

나는 아마도 멍청한 놈일뿐입니다. ._.

답변

15

당신은 Path.GetFileName(string)을 찾고 있습니다.

+0

내 코드 Path.GetFileNameWithoutExtension 필요하지만 감사 ... – CrimsonDeath

+5

그건 무슨 요 아니에요. 그러나 UR 질문은 말했다. – Joey

78

System.IO에는 파일 및 디렉토리와 작동하는 데 필요한 클래스가 다릅니다. 당신이 올바른 결과를 얻을 수있는 마지막 문자 검색으로

Path.GetExtension(yourPath); // returns .exe 
Path.GetFileNameWithoutExtension(yourPath); // returns File 
Path.GetFileName(yourPath); // returns File.exe 
Path.GetDirectoryName(yourPath); // returns C:\Program Files\Program 
+4

그리고'Path.GetDirectoryName (yourPath)'는 폴더 경로를 가져올 것입니다. –

2

을 : 그들 사이에 가장 유용한 일 중 하나는 파일 및 폴더 작업을위한 정적 도우미 메서드 많이 있습니다 Path입니다.

string path = "C:\\Program Files\\Program\\fatih.gurdal.docx"; 
string fileName = path.Substring(path.LastIndexOf(((char)92))+ 1); 
int index = fileName.LastIndexOf('.'); 
string onyName= fileName.Substring(0, index); 
string fileExtension = fileName.Substring(index + 1); 
Console.WriteLine("Full File Name: "+fileName); 
Console.WriteLine("Full File Ony Name: "+onyName); 
Console.WriteLine("Full File Extension: "+fileExtension); 

출력 :

전체 파일 이름 : fatih.gurdal.docx

전체 파일 ONY 이름 : fatih.gurdal

전체 파일 확장 : DOCX

관련 문제