-3

Possible Duplicate:
Get file name from a path string in C#

using : c# asp.net vs10

suppose, filepath = "C:\Sys\Axa_Excel\Axa123.xlsx". filepath is a string variable. file can be from whatever location. but the file will be always xls/xlsx/csv. How i have to get the file name only from the filepath which is just a string?

Community
  • 1
  • 1
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83
  • 2
    What have you tried? HInt: Look at the static methods of the FIle, Path and Directory classes- all what you Need is there, so you obviously did not try "learning C#". – TomTom Jan 09 '13 at 06:57
  • what are you trying to do ? – adt Jan 09 '13 at 06:57

3 Answers3

5

Use Path.GetFileName method.

string result = Path.GetFileName(@"C:\Sys\Axa_Excel\Axa123.xlsx");

How i have to get the file name only from the filepath which is just a string?

Use Path.GetFileNameWithoutExtension Method

string result = Path.GetFileNameWithoutExtension(@"C:\Sys\Axa_Excel\Axa123.xlsx");
Habib
  • 219,104
  • 29
  • 407
  • 436
0

Possible duplicate of multiple questions like:

However, to get the filename incl. extension, use the static method:

Path.GetFileName

Documented here.

To get the filename without extension, use the static method:

Path.GetFileNameWithoutExtension

Documented here.

Community
  • 1
  • 1
Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
0

Use Path.GetFileName Path.GetFileNameWithoutExtension static methods.

You need using System.IO to do that as well.

string filename = Path.GetFileName(@'C:\Sys\Axa_Excel\Axa123.xlsx');
string filenamewithoutextension = Path.GetFileNameWithoutExtension(@'C:\Sys\Axa_Excel\Axa123.xlsx');
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263