Solved

Get directory path

Posted on 2010-09-14
5
536 Views
Last Modified: 2012-05-10
Hello
With
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)
0
Comment
Question by:ICSAutomation
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
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.
0
 
LVL 53

Accepted Solution

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

http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.parent.aspx


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

Assisted Solution

by:AndyAinscow
AndyAinscow earned 20 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
0
 
LVL 5

Assisted Solution

by:Priya Perumpilavil
Priya Perumpilavil earned 20 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(); ;


        }
0
 
LVL 2

Expert Comment

by:sumeetsuvi
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();
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

19 Experts available now in Live!

Get 1:1 Help Now