web analytics

Using System.IO.Path class in C#

Options
@2015-12-18 09:17:30

Path.IsPathRooted and Path.GetPathRoot

Path.IsPathRooted method gets a value indicating whether the specified path string contains absolute or relative path information.

string fileName = @"C:\mydir\myfile.ext";
string UncPath = @"\\myPc\mydir\myfile";
string relativePath = @"mydir\sudir\";
bool result;

result = Path.IsPathRooted(fileName);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    fileName, result);

result = Path.IsPathRooted(UncPath);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    UncPath, result);

result = Path.IsPathRooted(relativePath);
Console.WriteLine("IsPathRooted('{0}') returns {1}",
    relativePath, result);

 This code produces output similar to the following:

 IsPathRooted('C:\mydir\myfile.ext') returns True
 IsPathRooted('\\myPc\mydir\myfile') returns True
 IsPathRooted('mydir\sudir\') returns False


Path.GetPathRoot method returns the root directory information for a path string.

string path = @"\mydir\";
string fileName = "myfile.ext";
string fullPath = @"C:\mydir\myfile.ext";
string pathRoot;

pathRoot = Path.GetPathRoot(path);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
    path, pathRoot);
pathRoot = Path.GetPathRoot(fileName);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
    fileName, pathRoot);

pathRoot = Path.GetPathRoot(fullPath);
Console.WriteLine("GetPathRoot('{0}') returns '{1}'",
    fullPath, pathRoot);

 This code produces output similar to the following:

 GetPathRoot('\mydir\') returns '\'
 GetPathRoot('myfile.ext') returns ''
 GetPathRoot('C:\mydir\myfile.ext') returns 'C:\'
@2016-01-09 18:12:42

Path.ChangeExtension Method

Path.ChangeExtension Method changes the extension of a path string.

public static string ChangeExtension(
   string path,
   string extension
)

For example:

String inputFile = @"..\..\InputFile.html";
String outputFile = Path.ChangeExtension(inputFile, ".pdf");

If path parameter contains a multiple extension separated by multiple periods, the returned string contains the contents of path with the last period and all characters following it replaced by extension. For example, if path is "\Dir1\examples\pathtests.csx.txt" and extension parameter is "cs", the modified path is "\Dir1\examples\pathtests.csx.cs".

@2017-10-17 23:00:37

How to get its directory path to a file?

Path.GetDirectoryName method returns the directory information for the specified path string.

The following code example demonstrates using the GetDirectoryName method on a Windows-based desktop platform.

string filePath = @"C:\MyDir\MySubDir\myfile.ext";
string directoryName;
int i = 0;

while (filePath != null)
{
    directoryName = Path.GetDirectoryName(filePath);
    Console.WriteLine("GetDirectoryName('{0}') returns '{1}'",
        filePath, directoryName);
    filePath = directoryName;
    if (i == 1)
    {
        filePath = directoryName + @"\";  // this will preserve the previous path
    }
    i++;
}

This code produces the following output:

GetDirectoryName( 'C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir'
GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir\') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir') returns 'C:\'
GetDirectoryName('C:\') returns ''

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com