Solved

Get directory path

Posted on 2010-09-14
5
535 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

20 Experts available now in Live!

Get 1:1 Help Now