2010-03-09 22 views
9

Tengo alguna ruta c:\server\folderName1\another name\something\another folder\.Obtener un nombre de carpeta de una ruta

¿Cómo puedo extraer de allí el último nombre de la carpeta?

He intentado varias cosas pero no funcionaron.

Simplemente no quiero buscar el último \ y luego tomar el resto.

Gracias.

Respuesta

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

DirectoryInfo.Name obras:

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 esta misma línea de comandos System.Linq:

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

También es posible usar System.IO.Path:

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