Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can get the filename without extension and path by using

C#
Path.GetFileNameWithoutExtension(filename);



But I want whole path without extension.

I want the following output as string


C#
C:\Users\Admin\Desktop\MyFileName


I want this to happen without extension.

How to do this
Posted
Comments
Philippe Mori 2-Sep-15 12:54pm    
Also get the directory and combine them again!

Easiest way?
C#
string path = @"D:\Temp\MyFile.txt";
string fullPathWithoutExtension = path.Substring(0, path.LastIndexOf('.'));
 
Share this answer
 
Comments
KUMAR619 28-Mar-14 0:50am    
Thanks sir its the best way
File name may contain multiple . so the above mentioned code is not safe at all the scenario.

Try below code.

C#
FileInfo fi = new FileInfo(@"C:\XMLFile1.txt");
Console.WriteLine(fi.DirectoryName +@"\" +fi.Name.Replace(fi.Extension,""));
Console.Read();
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900