-1

I am developing a program that functions similar to the MS-DOS command "cd". The user types "cd.." into the program's prompt and then, using Stringbuilder, remove the characters representing the user's current directory. It would function like this:

Prompt> cd..

with a Stringbuilder instance declared in the code file:

Stringbuilder builder = new Stringbuilder(@"E:\Folder");

After the user types "cd.." and hits Enter, the program would remove the directory at the end of the Stringbuilder value, with the end result like this:

Stringbuilder builder = new Stringbuilder(@"E:\");

I am not sure how I can do this, or if it is even possible to do. I thought I could try builder.Length - Directory.Length to get the length of the directory name, and then use builder.Remove(int StartIndex, int length), but I don't know how that could be implemented.

Another thing I could possibly use is using builder.ToString() and split it, using the backslash as the seperator.

Any suggestions?

Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
S. Ferrell
  • 171
  • 3
  • 11
  • 2
    Why are you using `StringBuilder` for this? Isn't it easier with `Substring` and `LastIndexOf`? – ProgrammingLlama Nov 12 '18 at 01:50
  • 1
    You can use: 'Path.GetDirectory' for that. – Poul Bak Nov 12 '18 at 01:55
  • Use the `Path` methods for constructing file paths, not string concatenation. The rules for constructing paths [are more complicated than you realize](https://stackoverflow.com/questions/1862993/). – Dour High Arch Nov 12 '18 at 02:10
  • I am trying not to use `LastIndexOf` for compatibility reasons – S. Ferrell Nov 12 '18 at 02:22
  • [LastIndexOf(char)](https://learn.microsoft.com/en-us/dotnet/api/system.string.lastindexof?view=netframework-4.7.2#applies-to) runs wherever C # can run. It is part of .net framework since 1.1. What compatibility are you talking about? – vasily.sib Nov 12 '18 at 03:45

1 Answers1

1

You could use Path.GetDirectoryName, but it's kind of dumb. It just does string manipulation with no idea what the file system actually looks like. For example, if you do Path.GetDirectoryName(@"E:\Folder\") it will return "E:\Folder", because there was a slash at the end of the string you gave it. Or you could feed it a folder that doesn't even exist, and it'll still give you an answer.

I can suggest another way to do this: keep a DirectoryInfo object for the current working directory. For example:

var curDirectory = new DirectoryInfo(@"E:\Folder");

When you need to display it, use curDirecotry.FullName, which will return the full path, like "E:\Folder". Then when you need to go up a directory, it's one line with no string manipulation:

curDirectory = curDirectory.Parent;

Although you do have to check it for null since, if you are already at the root of the drive, Parent will be null.

DirectoryInfo actually looks at the file system so you can be sure that it's always valid. Plus, you can use it for seeing the contents of the folder too, which I'm sure you will want to do.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84