Get directory path

Posted on 2010-09-14
Medium Priority
Last Modified: 2012-05-10
string paht = System.IO.Directory.GetCurrent Directory();
I get the Path: "D:\MyProjects\TestProject\UI\binDebug"

How can I get the Path "D:\MyProjects\TestProject" ? (is two levels higher)
Question by:ICSAutomation
LVL 53

Expert Comment

ID: 33671693
If I may ask: why do you need that path, because the first path has the name of the path where the exe resides.
When you deploy the application, it will contain the correct path.
LVL 53

Accepted Solution

Dhaest earned 80 total points
ID: 33671710
you need the "parent directory" (of the parent :))


DirectoryInfo di = New DirectoryInfo(System.IO.Directory.GetCurrent Directory().ToString());
DirectoryInfo pa = di.Parent.Parent
LVL 45

Assisted Solution

AndyAinscow earned 80 total points
ID: 33671736
That path returned is the current directory - you can modify the initial working directory in the project settings if you wished.
Typically the current directory is where the exe file is to be found BUT it can be different under certain circumstances.  If you want to use this to find where your exe is located on disc then it is a dangerous technique

Assisted Solution

by:Priya Perumpilavil
Priya Perumpilavil earned 80 total points
ID: 33672182
just try this
  private void button1_Click(object sender, EventArgs e)
            string fullpath = System.IO.Directory.GetCurrentDirectory().ToString();

            string Parents = System.IO.Directory.GetParent(fullpath).ToString();
            label2.Text = System.IO.Directory.GetParent(Parents).ToString(); ;


Expert Comment

ID: 33672476
Use as it one line code...

first add
using System.IO;
string path = System.IO.Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory().ToString()).ToString()).ToString();

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses

600 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