2012-10-22 23 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

यह नहीं है कि यो आपका सवाल कहा, यद्यपि। – 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