0

It seems a simple one but I confused to get it.

Here is the case:

I have a complete file name like abdcd.pdf or efghijf.jpg or jklmn.jpeg,

Now I have to get only the file name as abdcd or efghijf or jklmn

AbdulAziz
  • 5,868
  • 14
  • 56
  • 77
  • find the last index of the '.', and take the substring from the beginning to that index – Alessio Jan 29 '14 at 19:24
  • 1
    Seriously, why would you bother posting a question here when [Googling the title](https://www.google.nl/search?q=How+can+I+get+a+%27File+Name%27+ONLY+from+the+(File+Name+%2B+Extension)+using+substring+in+C%23) will get the answer as well... – venerik Jan 29 '14 at 19:33

7 Answers7

8

Use Path class static method

result = Path.GetFileNameWithoutExtension(fileName);
Mzf
  • 5,210
  • 2
  • 24
  • 37
Eric.Y.Fan
  • 236
  • 1
  • 5
2
        String f = "file.jpg";
        int lastIndex = f.LastIndexOf('.');
        Console.WriteLine(f.Substring(0, lastIndex));

Or, like the others suggested, you can also use

        Path.GetFileNameWithoutExtension(f)
Alessio
  • 2,018
  • 25
  • 26
  • 1
    looking up the last '.' will fail if the filename doesn't have a '.'. Following that the call `Substring(0,-1)` will throw an exception. GetFileNameWithoutExtension is th much better option. – Peter Jan 29 '14 at 21:36
  • yes you're right, it was assumed that the extension was always present I think ;) – Alessio Jan 29 '14 at 21:41
2

You could use String.Substring(), but I recommend Path.GetFileNameWithoutExtension() for this task:

// returns "test"
Path.GetFileNameWithoutExtension("test.txt")

Go to the msdn documentation

This method is essentially implemented like this:

  int index = path.LastIndexOf('.');
  return index == -1 ? path : path.Substring(0, index);
Elian Ebbing
  • 18,779
  • 5
  • 48
  • 56
2

I would use the API call.

http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension(v=vs.110).aspx

string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;

result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", 
    fileName, result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
    path, result);

// This code produces output similar to the following: 
// 
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile' 
// GetFileName('C:\mydir\') returns ''
asawyer
  • 17,642
  • 8
  • 59
  • 87
2

I would use Path static method: Path.GetFileNameWithoutExtension()

Moop
  • 3,414
  • 2
  • 23
  • 37
2

There is actually a method for that:

http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension(v=vs.110).aspx

Rick Love
  • 12,519
  • 4
  • 28
  • 27
2

Use the GetFileNameWithoutExtension static method like this:

result = Path.GetFileNameWithoutExtension(fileName);

From the MSDN:

The string returned by GetFileName, minus the last period (.) and all characters following it.

Mzf
  • 5,210
  • 2
  • 24
  • 37