-2

This is the line:

string t = Path.GetDirectoryName(file1);

The result is:

C:\\Users\\bout0_000\\AppData\\Local\\Extracting_Frames\\Extracting_Frames\\dat file\\converted.avi\\histogramValues.dat

I want it to only contain: converted.avi.

PS: converted.avi is not a file name it's a path name. I want to get the last part of the path the last subdirectory without the file name.

Nolonar
  • 5,962
  • 3
  • 36
  • 55
Jhonatan Birdy
  • 227
  • 2
  • 4
  • 13
  • Is `converted.avi` folder name or file name? – Hamlet Hakobyan Feb 01 '13 at 15:17
  • Hamlet Hakobyan my mistake I didn't explain that converted.avi is a directory name wich is a subdirectory. if this is file1: C:\\Users\\bout0_000\\AppData\\Local\\Extracting_Frames\\Extracting_Frames\\dat file\\converted.avi\\histogramValues.dat so I want to get the last subdirectory only without the file name. So t in the end will contain only: converted.avi wich is a directory not a file. – Jhonatan Birdy Feb 01 '13 at 15:23

5 Answers5

6

if converted.avi is the file name then use this

string t = Path.GetFileName(file1);

Docs: http://msdn.microsoft.com/en-gb/library/system.io.path.getfilename.aspx

However, if that is the name of a directory, you could just extract everything after the last backslash from the result you already have. eg

var path = Path.GetDirectoryName(fileName);
var parentFolder = path.Substring(path.LastIndexOf('\\')+1);

Live example: http://rextester.com/RYPMI91227

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • No my mistake it's not what I wanted. file1 contain: C:\\Users\\bout0_000\\AppData\\Local\\Extracting_Frames\\Extracting_Frames\\dat file\\converted.avi\\histogramValues.dat I want to get only the part of the path wich is: converted.avi – Jhonatan Birdy Feb 01 '13 at 15:18
  • converted.avi is not a file name it's a path name. I want to get the last part of the path the last subdirectory without the file name. – Jhonatan Birdy Feb 01 '13 at 15:19
  • @Jhonatan Birdy - then FileInfo might help – ScruffyDuck Feb 01 '13 at 15:21
  • No need to use SubString and LastIndexOf. Just use Path.GetFileName(path) – parvus Sep 11 '14 at 06:17
  • @parvus - did you actually *read* this answer, or the question for that matter? – Jamiec Sep 11 '14 at 07:52
  • @Jamiec Well... yes! I actually feel a bit stupid now that even after your reprimand I still don't see what's wrong with my remark. I forked your rextester: http://rextester.com/ETFH60769 – parvus Sep 11 '14 at 13:00
  • @parvus - My point was that I actually pointed out the use of `Path.GetFileName()`. Your point is somewhat valid, that the same method gets the first thing inside a path that vaguely looks like a file name. So it works - but kind of by luck not judgement. IMO, that method should be retrieving `histogramValues.dat` from the example given. – Jamiec Sep 11 '14 at 15:19
0

You want FileInfo.Name:

http://msdn.microsoft.com/en-us/library/system.io.fileinfo.name(v=vs.80).aspx

so something like:

var fileInfo = new FileInfo(file1);
var fileName = fileInfo.Name; // this will only contain converted.avi
Arran
  • 24,648
  • 6
  • 68
  • 78
  • This is not a good idea to use _FileInfo_ because it requires read permission on the specified file. (look at the exceptions thrown by [_FileInfo_ constructor](http://msdn.microsoft.com/en-us/library/system.io.fileinfo.fileinfo.aspx)) – Cédric Bignon Feb 01 '13 at 15:20
  • @CédricBignon, absolutely, OP hasn't said if that's a problem yet, a good overview of the two methods described in the various answers in this thread are also discussed here: http://stackoverflow.com/questions/4979762/new-fileinfopath-name-versus-path-getfilenamepath ... – Arran Feb 01 '13 at 15:24
0

Use Path.GetFilename will return the file name

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
0

Use File Info

FileInfo info = new FileInfo(yourFilePath);
var filename = info.Name;

http://msdn.microsoft.com/en-us/library/system.io.fileinfo(v=vs.100).aspx

ScruffyDuck
  • 2,606
  • 3
  • 34
  • 50
  • This is not a good idea to use _FileInfo_ because it requires read permission on the specified file. (look at the exceptions thrown by [_FileInfo_ constructor](http://msdn.microsoft.com/en-us/library/system.io.fileinfo.fileinfo.aspx)) – Cédric Bignon Feb 01 '13 at 15:20
0

Use the GetFileName() function of the Path class

whastupduck
  • 1,156
  • 11
  • 25