2010-03-09 18 views
9

Ho qualche percorso c:\server\folderName1\another name\something\another folder\.Ottieni un nome di cartella da un percorso

Come posso estrarre da lì l'ultimo nome della cartella?

Ho provato diverse cose ma non hanno funzionato.

Semplicemente non voglio cercare l'ultimo \ e poi prendere il resto.

Grazie.

risposta

15
string a = new System.IO.DirectoryInfo(@"c:\server\folderName1\another name\something\another folder\").Name; 
5

DirectoryInfo.Name opere:

using System; 
using System.IO; 

class Test 
{ 
    static void Main() 
    { 
     DirectoryInfo info = new DirectoryInfo("c:\\users\\jon\\test\\"); 
     Console.WriteLine(info.Name); // Prints test 
    }             
} 
0

uso questo comando System.Linq una riga:

foldername.Split(Path.DirectorySeparatorChar).Reverse().ToArray()[0] 
1

anche possibile utilizzando System.IO.Path:

string s = Path.GetFileName(Path.GetDirectoryName(@"c:\server\folderName1\another name\something\another folder\")); 
Problemi correlati