1

I have the following string

string a = @"\\server\MainDirectory\SubDirectoryA\SubdirectoryB\SubdirectoryC\Test.jpg";

I'm trying to remove part of the string so in the end I want to be left with

string a = @"\\server\MainDirectory\SubDirectoryA\SubdirectoryB";

So currently I'm doing

string b = a.Remove(a.LastIndexOf('\\'));
string c = b.Remove(b.LastIndexOf('\\'));
Console.WriteLine(c);

which gives me the correct result. I was wondering if there is a better way of doing this? because I'm having to do this in a fair few places.

Note: the SubdirectoryC length will be unknown. As it is made of the numbers/letters a user inputs

Kay Lee
  • 922
  • 1
  • 12
  • 40
Izzy
  • 6,740
  • 7
  • 40
  • 84
  • Do you always want to just go up 1 directory? – BugFinder Feb 14 '17 at 10:20
  • Or `SubdirectoryC` is always the same? – Pikoh Feb 14 '17 at 10:21
  • @BugFinder yes for now as far as I'm aware that is the case – Izzy Feb 14 '17 at 10:21
  • @Pikoh `SubdirectoryC` should never be the same – Izzy Feb 14 '17 at 10:22
  • Your example is not really explaining what you want to achieve. Can you describe it with some instructions, like "I want the code to go up two directories" or "I want the code to go up until it reaches a sub directory of some given name"? – Aziuth Feb 14 '17 at 10:32
  • One answer introducing CustomIndex in this post might be one of alternative ways. C# third index of a character in a string - Stack Overflow http://stackoverflow.com/questions/4578735/c-sharp-third-index-of-a-character-in-a-string/4578750#4578750 – Kay Lee Feb 14 '17 at 10:40

5 Answers5

8

There is Path.GetDirectoryName

string a = @"\\server\MainDirectory\SubDirectoryA\SubdirectoryB\SubdirectoryC\Test.jpg";
string b = Path.GetDirectoryName(Path.GetDirectoryName(a));

As explained in MSDN it works also if you pass a directory

....passing the returned path back into the GetDirectoryName method will result in the truncation of one folder level per subsequent call on the result string

Of course this is safe if you have at least two directories level

Steve
  • 213,761
  • 22
  • 232
  • 286
  • You've just made me look like a right idiot!.. Just as simple as that hey.. Thanks for this I can turn this into an helper method so I can re-use it. I'll accept the answer as soon as the time limit is up – Izzy Feb 14 '17 at 10:28
  • Don't be too harsh on yourself. Remembering everything available on the NET Framework (with the fine details) is not humanly possible. – Steve Feb 14 '17 at 10:34
  • Partially unrelated side note: if you want to get the name of `SubdirectoryB` you have to use `Path.GetFileName` which isn't the easiest to remember. – TheLethalCoder Feb 14 '17 at 10:37
0

Heyho,

if you just want to get rid of the last part. You can use :

var parentDirectory = Directory.GetParent(Path.GetDirectoryName(path));

https://msdn.microsoft.com/de-de/library/system.io.directory.getparent(v=vs.110).aspx

Tobias Theel
  • 3,088
  • 2
  • 25
  • 45
0

Some alternatives

string a = @"\\server\MainDirectory\SubDirectoryA\SubdirectoryB\SubdirectoryC\Test.jpg";

var b = Path.GetFullPath(a + @"\..\..");

var c = a.Remove(a.LastIndexOf('\\', a.LastIndexOf('\\') - 1));

but I do find this kind of string extensions generally usefull:

static string beforeLast(this string str, string delimiter)
{
    int i = str.LastIndexOf(delimiter);
    if (i < 0) return str;
    return str.Remove(i);
}
Slai
  • 22,144
  • 5
  • 45
  • 53
0

An alternative answer using Linq:

var b = string.Join("\\", a.Split(new string[] { "\\" }, StringSplitOptions.None)
                           .Reverse().Skip(2).Reverse());
Pikoh
  • 7,582
  • 28
  • 53
-1

For such repeated tasks, a good solution is often to write an extension method, e.g.

public static class Extensions
{
    public static string ChopPath(this string path)
    {
        // chopping code here
    }
}

Which you then can use anywhere you need it:

var chopped = a.ChopPath();
Peter B
  • 22,460
  • 5
  • 32
  • 69
  • This doesn't answer the question that was asked. The question is what to put inside of `ChopPath` not how to write an extension method. – TheLethalCoder Feb 14 '17 at 10:40
  • Please re-read, he asked: "Is there better way of doing this? because I'm having to do this in a fair few places." I proposed a better way, while not commenting on how to chop (because that worked) but how to avoid the redundancy that he was talking about. Asking for better does not mean that it can be improved in exactly one way, sometimes there are multiple aspects to it, one of which I am describing. – Peter B Feb 14 '17 at 12:07
  • The question boils down to "How can I get the directory path from a supplied string", it is just worded poorly. I see how you interpreted the question, I just don't believe that is what the OP was asking for and hence my downvote. – TheLethalCoder Feb 14 '17 at 12:14