0

I have a file location of the form FileName = "C:\Data\PronetContent\Content\Versions\14602\Working\1234.htm"

I require the part of the string except the htm file name - 1234.htm

So my desired result is "C:\Data\PronetContent\Content\Versions\14602\Working"

I implemented this code snippet:

string[] fileLocation = FileName.Split('/');
string[] fileLocation1 = fileLocation.Take(fileLocation.Count() - 1).ToArray();
string Floc = string.Join("/", fileLocation1);

But I am getting an empty string. Please help

nseries73
  • 1
  • 2
  • 3
    So, you are dealing with paths - why not use the `System.IO.Path` class with its static methods? They solve exactly this issue ... Another thing: please do not use `string.Join` or - more generally speaking - stirng concatenation for building up paths - rather use `System.IO.Path.Combine` –  May 10 '16 at 07:46
  • [`Path.GetDirectoryName()`](https://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname%28v=vs.110%29.aspx) – Matthew Watson May 10 '16 at 07:47
  • Or you use `FileInfo` to get the `FileName` and replace that with `""` – Felix D. May 10 '16 at 07:48
  • Use the debugger to inspect each step; then sort out what you need to do. Also, search SO for similar questions. This has been asked many times, e.g. [here](https://stackoverflow.com/questions/3826763/get-full-path-without-filename-from-path-that-includes-filename) – hlintrup May 10 '16 at 07:50
  • 2
    That is what the [MSDN](https://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx) documents are for. Am shocked at the sheer laziness in this question, we're not here to dish out code. Not alone that, you have access to MSDN, that is your go-to documentation which is far more extensive. – t0mm13b May 10 '16 at 07:51
  • @t0mm13b I actually missed that I could have done with the IO. Thanks for the help btw – nseries73 May 10 '16 at 08:26
  • You should also mark the answer as correct.. – BugFinder May 11 '16 at 08:19

2 Answers2

6

Firstly, using split on / for a windows based machine, will not split your string, because its split by \

You can use

Path.GetDirectoryName()

So,

Path.GetDirectoryName(FileName)

will return your path

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • Might embellish the answer highlighting the environment differences in the path separator. https://msdn.microsoft.com/en-us/library/system.io.path.directoryseparatorchar(v=vs.110).aspx – t0mm13b May 10 '16 at 07:58
  • 1
    @t0mm13b The `Path` class should implement the appropriate logic for the system it is operating on. On a *nix based system that uses '\' to separate paths the separator character is set appropriately and the `Path.GetDirectoryName` method should still work. Did you post your comment after reading the first line without reading the rest of the answer? – Corey May 10 '16 at 07:59
  • @Corey I removed my comment and added a suggested comment to indicate about the / and \ regardless of the `Path` handling the differences as a reminder and nudge about using string hacks to get at the directory components. – t0mm13b May 10 '16 at 08:02
  • @t0mm13b Since the OP is specifically working with Windows-style paths it is likely to be irrelevant to the question to mention *nix pathing rules, especially as the answer works perfectly well for both systems. – Corey May 10 '16 at 08:08
  • 1
    Yes. You really are lazy arent you – BugFinder May 10 '16 at 08:19
0

You have a Absolute path there, so if you are sure you will always have an absolute path, you can use the Path.GetDirectoryName(strPath) as below:

//absolute path
var filePath = @"C:\Data\PronetContent\Content\Versions\14602\Working\1234.htm";
var pathWithoutFileName = Path.GetDirectoryName(filePath);
monstertjie_za
  • 7,277
  • 8
  • 42
  • 73