MorkaLork Development

Interesting stuff I've picked up over the years...

Input/Output 7: Path

2009-04-16 21:28:42 | 175 views | csharp class method properties

The Basics

The Path class provides properties and methods that you can use to write portable applications, when your applications need to run on more than one operating system. As you may be aware, different operating systems have different directory and file naming conventions. For example, the Windows operating system recognizes the back-slash () character as a directory separator. In Unix or Linux, however, the forward slash (/) character is used to separate a directory from a subdirectory. By using the Path class, you can write applications that will run on multiple operating systems without changing your code. For example, the static field DirectorySeparatorChar translates into the directory separator character of the operating system on which the application is run. Therefore, if you need to access a file called flower.gif in a subdirectory named images, you can specify the following string as a relative path to the file:

string path = "images" + Path.DirectorySeparatorChar
+ "Flower.gif";

The following list give the other static fields.


Returns a character that is used to separate paths in the current operating system. For Windows, this is the semicolon (;) character.


Returns a character which is the alternate directory separator character for the current operating system. In Unix or Linux, this character is the back slash (). In Windows and Macintosh, this character is the forward slash (/).


Returns an array of characters that cannot be used in a path in the current operating system. For instance, in Windows, you cannot have a filename that contains an asterisk or a question mark. You can use this field to verify the validity of a filename entered by the user.


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.