10

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?

MadBoy
  • 10,824
  • 24
  • 95
  • 156

4 Answers4

33
return Path.GetFileNameWithoutExtension (fullpath);
Gonzalo
  • 20,805
  • 3
  • 75
  • 78
6

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

moi_meme
  • 9,180
  • 4
  • 44
  • 63
4

You're looking for Path.GetFileNameWithoutExtension

Tom
  • 1,269
  • 8
  • 12
3

One More solution

string fileName = new FileInfo(varFullPathToFile).Name;
fileName=fileName.Substring(0, fileName.LastIndexOf("."));
sandeep_jagtap
  • 1,484
  • 2
  • 17
  • 24