CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Get File Extension, File Name, File Name without Extension from Path in C# and ASP.Net
Submitted By Satheesh Babu B
On 12/24/2008 7:01:30 AM
Tags: ASP.Net,CodeDigest  

Get File Extension, File Name, File Name without Extension from Path in C# and ASP.Net

 

When we handle files in our project we will come across situations where we need to extract the extension or file name to do some manipulation or validation. Path class in System.IO namespace has a handful of utility methods that assist us in doing this with very less effort.

 

Refer the below code snippet which will help us to achieve it,

 

To get File Extension,

Response.Write(Path.GetExtension(@"F:\Articles\ValidationControls\ValidationControls.zip"));

 

To get File Name,

Response.Write(Path.GetFileName(@"F:\Articles\ValidationControls\ValidationControls.zip"));

 

To get File name without extension,

Response.Write(Path.GetFileNameWithoutExtension(@"F:\Articles\ValidationControls\ValidationControls.zip"));

 

 

Consider, we are creating the file name dynamically. If we want to check for path or file name invalid characters then we could use GetInvalidPathChars()and GetInvalidFileNameChars() method of Path class.

 

Path.GetInvalidPathChars()

Path.GetInvalidFileNameChars()

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..