0

I have a text file inside a drive. Probably inside a folder in the drive. I know the filename and drive name. I searches the file occurance through

var file = Directory.GetFiles(ftpPath,fileName,SearchOption.AllDirectories);

That is ok. But if the file is in a folder under the drive how could I get the ne path in ftpPath variable?

I could not find any valid solution for this.

Zigma
  • 529
  • 6
  • 37
  • Have you seen [this post](http://stackoverflow.com/questions/1225294/c-sharp-find-a-file-within-all-possible-folders). Seems like they are doing the same thing. – Jay Aug 07 '13 at 15:00
  • I have the Drive. I just need the full path if its under a folder. They are talking about filename – Zigma Aug 07 '13 at 15:02

2 Answers2

1

It should be something like:

ftpPath = Path.GetDirectoryName(file[0]);

Now the path in which filename was found in is stored in ftpPath. Of course you should add some checks before accessing file to ensure it was actually found...

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
0

To parse the path of a path + filename string use:

System.IO.Path.GetDirectoryName(ftpPath);

ftpPath = @"C:\Path\To\The\File\Filename.dll";
will output @"C:\Path\To\The\File"

Jay
  • 6,224
  • 4
  • 20
  • 23