• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Access directory?

Hi
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

c:\code\newcode2\pauls\help\subdir1\subdir2\subdir3

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

How will i do it>?

Thanks
Paul

0
paulwhelan
Asked:
paulwhelan
  • 3
1 Solution
 
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
di.Parent.Parent.Parent.Parent.FullName
Thanks
Paul
0
 
PoeticAudioCommented:
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
    if(path.EndsWith(@"\"))
    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);
MessageBox.Show(newDir);
0
 
PoeticAudioCommented:
Paul,

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.
0
 
PoeticAudioCommented:
OOPS!

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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now