2010-09-17 37 views
128
string path = "C:/folder1/folder2/file.txt"; 

Tôi có thể sử dụng các đối tượng hoặc phương pháp nào để cho kết quả là folder2?Lấy tên thư mục từ một đường dẫn

+5

Bạn có muốn tên thư mục cuối cùng vì vậy nếu bạn đã có C: \ folder1 \ folder2 \ folder3 \ file.txt, bạn muốn "folder3"? –

Trả lời

238

tôi có lẽ sẽ sử dụng một cái gì đó như:

string path = "C:/folder1/folder2/file.txt"; 
string lastFolderName = Path.GetFileName(Path.GetDirectoryName(path)); 

Cuộc gọi bên trong để GetDirectoryName sẽ trở lại đường dẫn đầy đủ, trong khi cuộc gọi bên ngoài để GetFileName() sẽ trở lại với thành phần đường dẫn mới nhất - đó sẽ là tên thư mục.

Cách tiếp cận này hoạt động có hay không đường dẫn thực sự tồn tại. Cách tiếp cận này, tuy nhiên, dựa vào đường dẫn ban đầu kết thúc bằng một tên tập tin. Nếu không biết liệu đường dẫn có kết thúc bằng tên tệp hoặc tên thư mục hay không - thì nó yêu cầu bạn kiểm tra đường dẫn thực tế để xem tệp/thư mục có tồn tại ở vị trí đầu tiên hay không. Trong trường hợp đó, câu trả lời của Dan Dimitru có thể phù hợp hơn.

+0

Cảm ơn LBushkin. Bí quyết đẹp. Tôi đã tìm kiếm điều này. –

+81

Một giải pháp khác: trả về DirectoryInfo mới (fullPath) .Name; –

+1

Thiên tài, thiên tài thuần khiết! Cảm ơn! –

-3
// For example: 
String[] filePaths = Directory.GetFiles(@"C:\Nouveau dossier\Source"); 
String targetPath = @"C:\Nouveau dossier\Destination"; 

foreach (String FileD in filePaths) 
{ 
    try 
    { 
    FileInfo info = new FileInfo(FileD); 
    String lastFolderName = Path.GetFileName(Path.GetDirectoryName(FileD)); 

    String NewDesFolder = System.IO.Path.Combine(targetPath, lastFolderName); 
    if (!System.IO.Directory.Exists(NewDesFolder)) 
    { 
     System.IO.Directory.CreateDirectory(NewDesFolder); 
    } 
    String destFile = System.IO.Path.Combine(NewDesFolder, info.Name); 

    File.Move(FileD, destFile); 

    // Try to move 
    Console.WriteLine("Moved"); // Success 
    } 
    catch (IOException ex) 
    { 
    Console.WriteLine(ex); // Write error 
    } 
} 
+1

Điều này liên quan đến câu hỏi như thế nào? – smiron

5

tôi đã sử dụng đoạn mã này để có được thư mục cho một con đường khi không có tên tập tin là trong đường dẫn:

ví dụ "c: \ tmp \ test \ visual";

string dir = @"c:\tmp\test\visual"; 
Console.WriteLine(dir.Replace(Path.GetDirectoryName(dir) + Path.DirectorySeparatorChar, "")); 

Output:

thị giác

+0

Bạn chỉ có thể thực hiện Path.GetFileName (dir) và nó sẽ trả về tên thư mục tốt. – jrich523

15

Hãy thử điều này:

string filename = @"C:/folder1/folder2/file.txt"; 
string FolderName = new DirectoryInfo(System.IO.Path.GetDirectoryName(filename)).Name; 
1

Dưới đây đang giúp để có được tên thư mục chỉ

 

public ObservableCollection items = new ObservableCollection(); 

    try 
      { 
       string[] folderPaths = Directory.GetDirectories(stemp); 
       items.Clear(); 
       foreach (string s in folderPaths) 
       { 
        items.Add(new gridItems { foldername = s.Remove(0, s.LastIndexOf('\\') + 1), folderpath = s }); 

       } 

      } 
      catch (Exception a) 
      { 

      } 
    public class gridItems 
    { 
     public string foldername { get; set; } 
     public string folderpath { get; set; } 
    } 
2
var fullPath = @"C:\folder1\folder2\file.txt"; 
var lastDirectory = Path.GetDirectoryName(fullPath).Split('\\').LastOrDefault(); 
-1

này là xấu xí nhưng tránh phân bổ:

private static string GetFolderName(string path) 
{ 
    var end = -1; 
    for (var i = path.Length; --i >= 0;) 
    { 
     var ch = path[i]; 
     if (ch == System.IO.Path.DirectorySeparatorChar || 
      ch == System.IO.Path.AltDirectorySeparatorChar || 
      ch == System.IO.Path.VolumeSeparatorChar) 
     { 
      if (end > 0) 
      { 
       return path.Substring(i + 1, end - i - 1); 
      } 

      end = i; 
     } 
    } 

    if (end > 0) 
    { 
     return path.Substring(0, end); 
    } 

    return path; 
} 
4

Simple & sạch. Chỉ sử dụng System.IO.FileSystem - công trình như một say mê:

string path = "C:/folder1/folder2/file.txt"; 
string folder = new DirectoryInfo(path).Name; 
1

DirectoryInfo hiện công việc để tước tên thư mục

string my_path = @"C:\Windows\System32"; 
DirectoryInfo dir_info = new DirectoryInfo(my_path); 
string directory = dir_info.Name; // System32 
Các vấn đề liên quan