Этот класс работает с путями к директориям и файлам.
Подключить пространство имен System.IO.
Подключить пространство имен System.IO.
наиболее часто используемые члены класса |
|
| Combine() | объединяет несколько строк в один путь |
| GetDirectoryName() | возвращает путь к директории, в которой находится файл |
| GetFileName() | возвращает имя файла с расширением |
| GetFileNameWithoutExtension() | возвращает имя файла без расширения |
| GetExtension() | возвращает расширение файла |
| GetPathRoot() | возвращает корневую директорию |
Combine()
Метод объединяет несколько строк в путь.
Main.cs
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringdirectory =@"D:\zzz"; -
stringfile ="a.txt"; -
stringpath = Path.Combine(directory, file); -
Console.WriteLine(path); -
Console.ReadKey(); -
return0; -
} }
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringdirectory =@"D:\zzz"; -
stringfile ="a.txt"; -
stringpath = Path.Combine(directory, file); -
Console.WriteLine(path); -
Console.ReadKey(); -
return0; -
} }
D:\zzz\a.txt
GetDirectoryName()
Метод возвращает путь к директории, в которой находится файл.
Main.cs
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringdirectory = Path.GetDirectoryName(path); -
Console.WriteLine(directory); -
Console.ReadKey(); -
return0; -
} }
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringdirectory = Path.GetDirectoryName(path); -
Console.WriteLine(directory); -
Console.ReadKey(); -
return0; -
} }
D:\zzz
GetFileName()
Метод возвращает имя файла с расширением.
Main.cs
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringfile = Path.GetFileName(path); -
Console.WriteLine(file); -
Console.ReadKey(); -
return0; -
} }
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringfile = Path.GetFileName(path); -
Console.WriteLine(file); -
Console.ReadKey(); -
return0; -
} }
a.txt
GetFileNameWithoutExtension()
Метод возвращает имя файла без расширения.
Main.cs
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringfile = Path.GetFileNameWithoutExtension(path); -
Console.WriteLine(file); -
Console.ReadKey(); -
return0; -
} }
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringfile = Path.GetFileNameWithoutExtension(path); -
Console.WriteLine(file); -
Console.ReadKey(); -
return0; -
} }
a
GetExtension()
Метод возвращает расширение файла.
Main.cs
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringfile = Path.GetExtension(path); -
Console.WriteLine(file); -
Console.ReadKey(); -
return0; -
} }
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringfile = Path.GetExtension(path); -
Console.WriteLine(file); -
Console.ReadKey(); -
return0; -
} }
.txt
GetPathRoot()
Метод возвращает корневую директорию.
Main.cs
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringroot = Path.GetPathRoot(path); -
Console.WriteLine(root); -
Console.ReadKey(); -
return0; -
} }
usingSystem;//подключить пространство именusingSystem.IO;classProgram {-
publicstaticintMain() { -
stringpath =@"D:\zzz\a.txt"; -
stringroot = Path.GetPathRoot(path); -
Console.WriteLine(root); -
Console.ReadKey(); -
return0; -
} }
D:\