Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return filename without extension from full path in C# [duplicate]

Tags:

c#

c#-4.0

I'm looking for a way to return fileName from full path but without extension.

    private static string ReturnFileNameWithoutExtension(string varFullPathToFile) {
        string fileName = new FileInfo(varFullPathToFile).Name;
        string extension = new FileInfo(varFullPathToFile).Extension;
        return fileName.Replace(extension, "");   
    }

Is there more bullet proof solution then replacing extension with empty string?

like image 695
MadBoy Avatar asked Apr 11 '11 17:04

MadBoy


4 Answers

One More solution

string fileName = new FileInfo(varFullPathToFile).Name;
fileName=fileName.Substring(0, fileName.LastIndexOf("."));
like image 88
sandeep_jagtap Avatar answered Oct 07 '22 14:10

sandeep_jagtap


return Path.GetFileNameWithoutExtension (fullpath);
like image 31
Gonzalo Avatar answered Oct 07 '22 14:10

Gonzalo


i'm using System.IO.Path.GetFileNameWithoutExtension(filename);

like image 22
moi_meme Avatar answered Oct 07 '22 15:10

moi_meme


You're looking for Path.GetFileNameWithoutExtension

like image 32
Tom Avatar answered Oct 07 '22 13:10

Tom