To find the root directory of a file in C#, you can use the Path.GetPathRoot(string path)
method provided by the System.IO
namespace. This method returns the root directory of the specified path as a string.
Here's an example of how to use it:
main.cs190 chars7 lines
Output:
main.cs32 chars2 lines
In this example, the filePath
variable contains the full path to the file "file.txt". The Path.GetPathRoot()
method is called with this path, and the return value is stored in the rootDirectory
variable. The output displays the root directory of the file, which is "C:" in this case.
gistlibby LogSnag