web analytics

Using System.IO.Path class in C#

Options

codeling 1602 - 6666
@2015-12-14 08:52:53

The Path class performs operations on String instances that contain file or directory path information. The members of the Path class enable you to quickly and easily perform common operations such as determining whether a file name extension is part of a path, and combining two strings into one path name. 

Path.GetExtension method gets the extension of the specified path

Have a filepath and want to know the extension of the file? The Path.GetExtension method will return precisely that. The extension of path is obtained by searching path for a period (.), starting with the last character in path and continuing toward the start of path. If a period is found before a DirectorySeparatorChar or AltDirectorySeparatorChar character, the returned string contains the period and the characters after it; otherwise, Empty is returned.

string fileName = @"C:\mydir.old\myfile.ext";
string path = @"C:\mydir.old\";
string extension;

//returns ".ext"
extension = Path.GetExtension(fileName);

//returns ""

extension = Path.GetExtension(path);
@2015-12-14 08:54:40

Path.Combine Method

The Path.Combine method combines two path strings. For example:

string part1 = "c:\\temp";

string part2 = "mytemp.txt";
Path.Combine(part1, part2);

The result will be

c:\temp\mytemp.txt

 If path2 includes a root, path2 is returned.

string part1 = "c:\\temp";

string part2 = "\\mytemp.txt";
Path.Combine(part1, part2);

The result will be

\mytemp.txt
@2015-12-18 00:39:11

Path.GetTempFileName and Path.GetRandomFileName Method

Path.GetTempFileName method creates a uniquely named, zero-byte temporary file in Windows temporary folder and returns the full path of that file.

This method gives you a very easy way of creating temporary file in case when you need small number of temporary files for short period of time. When there are 65536 files it gives an IOException saying "The file exists.", so you have to keep cleaning temporary files you created on regular basis.

Unlike GetTempFileName, the Path.GetRandomFileName method returns a cryptographically strong, random string that can be used as either a folder name or a file name but it does not create a file in the temp folder.

Use one of the existing methods for generating unique name for temporary files:

  • Path.GetTempFileName() - use this method if you want to create temporary file in user's temp folder.
  • Path.GetRandomFileName() - use this method if you just want to generate unique file name.
@2015-12-18 00:44:46

Path.GetInvalidFileNameChars and Path.GetInvalidPathChars

Path.GetInvalidFileNameChars method gets an array containing the characters that are not allowed in file names.

Path.GetInvalidPathChars method gets an array containing the characters that are not allowed in path names.

The following C# code snippest shows you how to use the above two methods to remove illegal path and file characters from a simple string:

string filepath = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";

foreach (char c in Path.GetInvalidFileNameChars())
{
    filepath = filepath.Replace(c.ToString(), "");
}
foreach (char c in Path.GetInvalidPathChars())
{
    filepath = filepath.Replace(c.ToString(), "");
}

Or using Regex

string filepath = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string regexSearch = string.Format("{0}{1}",
                     new string(Path.GetInvalidFileNameChars()),
                     new string(Path.GetInvalidPathChars()));
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
filepath = r.Replace(filepath, "");

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com