Click here to Skip to main content
Click here to Skip to main content

Really Useful .NET Classes: Part 1 - System.IO.Path

, 27 Jun 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
First article in a series about useful classes in .NET.

Introduction

There are hundreds and hundreds of classes in the .NET Framework, and there are some that are really useful which you may never come across. I've been a .NET programmer for several years and I still run across classes in the framework that I've never seen before. I thought it would be cool to document some of the really useful (in my opinion) classes because other programmers may never have known they existed.

System.IO.Path

System.IO.Path is a static class that provides many useful operations on file or directory path information in a cross-platform manner. Cross-platform manner? Yes, you can run .NET code on non-Windows machines. In case you don't know, check out Mono, which is a .NET implementation for Linux, Solaris, Mac, and Windows. There are several cool articles right here on CodeProject about Mono - just do a search on "mono". If you're writing cross-platform code, being cognizant of your platform is key and System.IO.Path can help.

The System.IO.Path class operates on strings, not on actual FileInfo, DirectoryInfo, or Stream objects. Most of the methods act on a string or return a string. The actual work to create the file needs to be done with extra code.

How do I find the temporary directory?

string temppath = Path.GetTempPath();
// will generate something like
// C:\Documents and Settings\youraccount\Local Settings\Temp

How do I generate a random file name?

string randomFile = Path.GetRandomFileName();
// will generate something like utbnd0fm.uyp or ewghyx4x.cvd

How do I generate a random text file?

You can use the ChangeExtension() method in conjunction with the GetRandomFileName() method.

string temptext = Path.ChangeExtension(Path.GetRandomFileName(), ".txt");
// generates something like pe054llb.txt

How do I get a unique temp file?

Ever need to generate a uniquely named file for some temporary operation? GetTempFileName() will actually generate a 0 byte file on the file system and return you the name.

string tempfile = Path.GetTempFileName();
// will generate something like
// C:\Documents and Settings\youraccount\Local Settings\Temp\tmp62.tmp

How do I get the file name from a full path?

string filepath = "c:\\windows\\somefile.txt"
file = Path.GetFileName(filepath); // returns just somefile.txt

How do I get the directory name from a file path?

string filepath = "c:\\windows\\somefile.txt"
file = Path.GetDirectoryName(filepath); // returns just c:\windows

How do I get the file extension?

string filepath = "c:\\windows\\somefile.txt"
file = Path.GetExtension(filepath); // returns just .txt

How do I find invalid file name characters?

GetInvalidFileNameChars() will return an array of chars that are invalid, to use in a file name. You can use this to check a user supplied file name before attempting to write a file to disk. There is a corresponding method for invalid path chars as well: GetInvalidPathChars().

string userfile = "junk.t>t";

char[] userchars = userfile.ToCharArray();
char[] invalids = Path.GetInvalidFileNameChars();

foreach (char c in userchars)
{
    foreach (char i in invalids)
    {
        if (c == i)
            throw new Exception("File name contains invalid chars!");
    }
}

Conclusion

I didn't cover every last System.IO.Path method or property, but I hit some of the big ones. I'm going to attempt to run these on my Linux box running Mono and update the article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jeff Modzel
Web Developer
United States United States
Technical Architect
Sungard HE

Comments and Discussions

 
Generalrelated to path.getfilename Pinmemberlijjat20-Dec-09 21:08 
GeneralThank you for this! PinmemberMember 337866729-Oct-08 7:18 
GeneralExcellent Article Pinmembercykophysh3919-Sep-06 11:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.1 | Last Updated 27 Jun 2006
Article Copyright 2006 by Jeff Modzel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid