web analytics
Using System.IO.Path class in C# Options
codeling
Posted: Friday, December 18, 2015 9:17:30 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 919
Points: 3861

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:\'
Sponsor
Posted: Friday, December 18, 2015 9:17:30 AM
codeling
Posted: Saturday, January 9, 2016 6:12:42 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 919
Points: 3861

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".

codeling
Posted: Tuesday, October 17, 2017 11:00:37 PM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 919
Points: 3861

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 ''
Users browsing this topic
Guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2017 Digcode.com. All rights reserved.