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,529
  • 21
  • 90
  • 151

4 Answers4

32
return Path.GetFileNameWithoutExtension (fullpath);
Gonzalo
  • 20,192
  • 3
  • 72
  • 77
6

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

moi_meme
  • 8,748
  • 4
  • 45
  • 61
3

One More solution

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

You're looking for Path.GetFileNameWithoutExtension

Tom
  • 1,229
  • 8
  • 12