Access directory?

This works fine

Help.ShowHelp(null, "C:\\code\\newcode2\\pauls\\help\\help.chm");

But I have a string

sStartupPath = Application.StartupPath

and when I display this it says


Now I know I will always need to back up three directories.

How will i do it>?


Who is Participating?
PoeticAudioConnect With a Mentor Commented:
I just threw this together, so there may be a better way, but this routine has the advantage of not needing to go through directory information (ie, you may want a path of something that doesn't actually exist on the machine your on) and also you specify how many subdirectories you want to move back. I made it so that it accepts directories that use forward-slashes, back-slashes and the directory can either end in a slash or not. ("C:\test\" should be the same as "C:\test") It worked through well for me under numerous tests, but there is a chance that I missed something.

Look under this method for sample code.

private string GetSubdirectory(string path, int numSubDirectoriesFromRoot)
    //if the path ends with a slash "\" then remove it for correct parsing
    path = path.Remove(path.Length - 1, 1);            }

    //if the path comes in the format like "C:/whatever/whatever"...etc then
    //replace the "/" with "\" so we can parse correctly
    path = path.Replace(@"/", @"\");

    char[] delim = {'\\'};

    //split the directory based on the delim character
    string[]directories = path.Split(delim);
    string newPath = "";

    //now piece the path back together based on how many subdirectories of the original
    //path the user wants to move back
    for(int i = 0; i < directories.Length - numSubDirectoriesFromRoot; i++)
        newPath += directories[i] + @"\";

     return newPath;

Then you can use it like:

string dir = @"c:/code/newcode2/pauls/help/subdir1/subdir2/subdir3/";
string newDir = GetSubdirectory(dir, 3);
paulwhelanAuthor Commented:
I got it working this way

MessageBox.Show("Application.StartupPath is " + Application.StartupPath);

            DirectoryInfo di = new DirectoryInfo(Application.StartupPath);
            MessageBox.Show("di.Parent.Parent.Parent.Parent is " + di.Parent.Parent.Parent.Parent);
            MessageBox.Show("di.Parent.Parent.Parent.Parent.FullName is " + di.Parent.Parent.Parent.Parent.FullName);

            Help.ShowHelp(null, di.Parent.Parent.Parent.Parent.FullName + "\\Help\\help.chm");

Is there a better way?

I dont like the

Not that it's much of my business as I am not a moderator... but you have TONS of open questions. Just wanted to warn you that people on this site will start to offer less and less help if they see that you have that many open questions with almost all of them being such a low point value.

really poor naming

private string GetSubdirectory(string path, int numSubDirectoriesFromRoot)

should be

private string GetSubdirectory(string path, int numSubDirectoriesFromPath)

because we are getting the subdirectories based on the path, not on the root directory. My fault.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.