Đầu tiên, đặt một chuỗi, tức là đường dẫn thư mục Windows của bạn -
string str = @"D:\Downloads\Amit";
Bây giờ, hãy sử dụng phương thức Split () và tách bất cứ nơi nào \ xảy ra -
str.Split(' \\') Sau đây là mã hoàn chỉnh -
Ví dụ
using System;
class Program {
static void Main() {
string str = @"D:\Downloads\Amit";
Console.WriteLine("Directory...\n"+str);
string[] myStr = str.Split('\\');
Console.WriteLine("\nSplit...");
foreach (string ch in myStr) {
Console.WriteLine(ch);
}
}
} Đầu ra
Directory... D:\Downloads\Amit Split... D: Downloads Amit