2013-04-17 47 views
10

Làm thế nào tôi có thể lấy thư mục gốc của một thư mục +1?Lấy thư mục gốc của một thư mục +1

Ví dụ: Input: C:\Level1\Level2\level3 sản lượng nên là:

Level1 

Nếu đầu vào là Level1 đầu ra nên Level1

nếu đầu vào là C: \ đầu ra nên empty string

Is có một chức năng. Net xử lý này?

sẽ luôn trả về C:\

+3

Không, không có chức năng tích hợp để xử lý việc này. –

Trả lời

11

Bạn có thể sử dụng Path-class + Substring + Split để xóa gốc và nhận thư mục trên cùng.

// your directory: 
string dir = @"C:\Level1\Level2\level3";  

// C:\ 
string root = Path.GetPathRoot(dir); 

// Level1\Level2\level3: 
string pathWithoutRoot = dir.Substring(root.Length);  

// Level1 
string firstFolder = pathWithoutRoot.Split(Path.DirectorySeparatorChar).First(); 

Một cách khác là sử dụng lớp DirectoryInfo và nó Parent tài sản:

DirectoryInfo directory = new DirectoryInfo(@"C:\Level1\Level2\level3"); 
string firstFolder = directory.Name; 
while (directory.Parent != null && directory.Parent.Name != directory.Root.Name) 
{ 
    firstFolder = directory.Parent.Name; 
    directory = directory.Parent; 
} 

Tuy nhiên, tôi muốn các phương pháp chuỗi "nhẹ".

0

Một giải pháp khả thi, nhưng có thể không phải là tốt nhất, là để tìm ra vị trí của @ "\", và làm một số thủ công chế biến cho mình. Mã dưới đây không được kiểm tra đầy đủ, chỉ cần đoạn mã:

//var positions = inputString.IndexOfAny(new [] {'\'}); //Original one 
//Updated, thanks to Snixtor's implementation 
var positions = inputString.IndexOfAny(new [] {Path.DirectorySeparatorChar}); 
int n=positions.Length; 
if(n>=1) 
{ 
    var pos = positions[1]; //The 2nd '\'; 
    return inputString.SubString(0, pos); 
} 
return null; 

Tất nhiên, điều này chỉ hoạt động nếu chúng tôi chắc chắn chúng tôi muốn có các nền conp sau thứ 2 '\'.

+0

Điều gì xảy ra nếu đầu vào sử dụng '/'? –

+0

new [] {'\', '/', ... 'của người khác', ...}) – David

1

Không chắc cho dù đây là cách chính xác nhưng bạn làm:

string s = @"C:\Level1\Level2\Level3"; 
string foo = s.split(@"\")[1]; 

Không chắc liệu DirectoryInfo đối tượng có thể có được điều này một cách sạch ..

DirectoryInfo di = new DirectoryInfo(@"C:\Level1\Level2\Level3"); 
di.Root; 
+2

'di.Root' sẽ trả về" C: \ ". –

4
string dir = @"C:\foo\bar\woah"; 
var dirSegments = dir.Split(new char[] { Path.DirectorySeparatorChar }, 
          StringSplitOptions.RemoveEmptyEntries); 
if (dirSegments.Length == 1) 
{ 
    return string.Empty; 
} 
else 
{ 
    return dirSegments[0] + Path.DirectorySeparatorChar + dirSegments[1]; 
} 
+0

Ah chúng ta đều thích thử thách mã hóa nhanh phải không? 4 câu trả lời tất cả trong vòng 4 phút :) – Snixtor

+0

Các bạn quá nhanh ...: P –

+0

Bạn cũng nên bao gồm cả 'AltDirectorySeparatorChar'. –

0

Bạn có thể lặp sử dụng lớp thông tin thư mục bằng cấu trúc sau bằng cách thêm phần mã bên dưới vào phương thức

string path = "C:\Level1\Level2\level3"; 
DirectoryInfo d = new DirectoryInfo(path); 
while (d.Parent.FullName != Path.GetPathRoot(path)) 
{ 
    d = d.Parent; 
} 
return d.FullName; 
0

Một hạnh phúc LINQ lót:

string level1_2 = Directory.GetDirectoryRoot(path) + path.Split(Path.DirectorySeparatorChar).Skip(1).Take(1).DefaultIfEmpty("").First(); 
+0

Ồ, tôi vừa để ý cái đầu tiên thất bại nếu nó chỉ là @ "C: \ Level1", vì vậy tôi đã gỡ bỏ nó. –

0
var di = new System.IO.DirectoryInfo(@"C:\a\b\c"); 
    Func<DirectoryInfo, DirectoryInfo> root = null; 
    root = (info) => info.Parent.FullName.EndsWith("\\") ? info : root(info.Parent); 
    var rootName = root(di).Name; //#a 
0

Tại sao không chỉ cần sử dụng System.IO.Path để lấy tên?

MessageBox.Show(System.IO.Path.GetFileName(
    System.IO.Path.GetDirectoryName(
     System.IO.Path.GetDirectoryName(@"C:\Level1\Level2\Level3") 
    ) 
)); 

Điều này trả về Level 1.

MessageBox.Show(System.IO.Path.GetFileName(
    System.IO.Path.GetDirectoryName(
     System.IO.Path.GetDirectoryName(@"C:\") 
    ) 
)); 

Chuỗi này trả về chuỗi trống.

1

Bạn có thể sử dụng DirectoryInfo và vòng lặp while.

DirectoryInfo info = new DirectoryInfo(path); 
while (info.Parent != null && info.Parent.Parent != null) 
    info = info.Parent; 
string result = info.FullName; 
Các vấn đề liên quan