DESKTOP

Manipulate File Paths

9/25/2010 3:19:45 PM
Problem : You need to combine a filename with a directory name, or extract certain parts of a path.

Solution:
You should almost never need to manually parse a path in C#. Instead, use the System.IO.Path class to perform your manipulation. This class has only static methods.


Table 1 details most of the methods and properties available to you.

Table 1. Path Methods and Properties
MethodInputOutput
GetDirectoryName()C:\Windows\System32\xcopy.exeC:\Windows\System32
GetExtension()C:\Windows\System32\xcopy.exe.exe
GetFileName()C:\Windows\System32\xcopy.exexcopy.exe
GetFileNameWithoutExtension()C:\Windows\System32\xcopy.exexcopy
GetFullPath()xcopy.exeC:\Windows\System32\xcopy.exe
Root()C:\Windows\System32\xcopy.exeC:\
HasExtension()C:\Windows\System32\xcopy.exeTrue
IsPathRooted()C:\Windows\System32\xcopy.exeTrue
IsPathRooted()xcopy.exeFalse
RandomFileName()N/Arq33lkoe.vwi
GetInvalidFileNameChars()N/AA long list of characters that are not allowed in filenames
GetInvalidPathChars()N/AA long list of characters that are not allowed in paths
AltDirectorySeparatorCharN/A‘/’
DirectorySeparatorCharN/A‘\’
PathSeparatorN/A‘;’
VolumeSeparatorCharN/A‘:’

There are two additional useful methods. Path.Combine() will take two or more strings and combine them into a single path, inserting the correct directory separator characters where necessary. Here’s an example:

string path = Path.Combine(@"C:\Windows\", "System32", "xcopy.exe ");

This results in the path having the value "C:\Windows\System32\xcopy.exe ".

The second method, Path.ChangeExtension(), does just what you think it would. Here’s an example:

string path = Path.ChangeExtension(@"C:\Windows\System32\xcopy.exe ", "bin");

The new value of path is "C:\Windows\System32\xcopy.bin". Note that this does not change the file on the disk—it’s just in the string.
Other  
 
Top 10
.NET Debugging : Introduction to the Tools - Reflector for .NET, PowerDbg, Managed Debugging Assistants
.NET Debugging : Introduction to the Tools - CLR Profiler, Performance Counters
Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
Canon Powershot D20 - Super-Durable Waterproof Camera
Fujifilm Finepix F800EXR – Another Excellent EXR
Sony NEX-6 – The Best Compact Camera
Teufel Cubycon 2 – An Excellent All-In-One For Films
Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
Philips 55PFL6007T With Fantastic Picture Quality
Philips Gioco 278G4 – An Excellent 27-inch Screen
Most View
Acrobat XI Pro - Make Your Job Easier Every Day
Non-Deterministic Finite Automata
AMD Trinity the Chip to Take down Core i3? (Part 2)
How To … Paint Your Case (Part 2)
SQL Server 2005 : Advanced OLAP - Translations
Smarter, Sharper and Snappier : Samsung NX200
Optimizing for Vertical Search : Optimizing for News, Blog, and Feed Search
Windows Server 2003 : Protecting Network Communications with Internet Protocol Security - IPSec Basics (part 1) - Security Advantages of IPSec
# BlackBerry Java Application Development : Networking - HTTP Basics
Tech Briefing: AMD Gets ARM’d
Building and Deploying Applications for Windows Azure : Creating a Demo Project
Windows Server 2008 : Domain Name System and IPv6 - Other DNS Components
FM Transmitters
World Wired Web (Part 2) - Windows 8 Flavours Unveiled
FujiFilm FinePix HS30 EXR Digital Camera - Super Zoom
Configure Server Core Postinstallation
Compact Camera With Significant Sensor
Flashy Preview
Sharepoint 2007: Approve or Reject a File or List Item
ASP.NET 4 : Data Source Controls (part 1) - The Page Life Cycle with Data Binding, The SqlDataSource, Selecting Records