File path fetch function and file name fetch function summary in C

  • 2020-12-16 06:05:26
  • OfStack

1. Get the absolute file path


System.IO.Path.GetFullPath(string path) string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath; fullPath = Path.GetFullPath(path1);
fullPath = Path.GetFullPath(fileName);
fullPath = Path.GetFullPath(path2);

2. Gets the file name (gets the file name in the specified path, excluding the extension)

System.IO.Path.GetFileNameWithoutExtension(string path)
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileNameWithoutExtension(fileName);
result = Path.GetFileName(path);

3. Get an string containing path directory information or an empty reference

System.IO.Path.GetDirectoryName(string path) string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string rootPath = @"C:\";
string directoryName;
   
directoryName = Path.GetDirectoryName(fileName);
directoryName = Path.GetDirectoryName(path);
directoryName = Path.GetDirectoryName(rootPath);

4. Merge two path strings.

System.IO.Path.Combine(String path1, String path2)


Related articles: