Access directory?

Posted on 2006-05-04
Last Modified: 2010-04-16
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>?


Question by:paulwhelan

    Author Comment

    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
    LVL 6

    Accepted Solution

    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);
    LVL 6

    Expert Comment


    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.
    LVL 6

    Expert Comment


    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now