Click here to Skip to main content
15,913,333 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,

I'm sorry if my title is not correct... I just want to know how will you be able to manipulate your current directory location? Here is the scenario:

Assuming that my project is located in c://myfiles/program/myproject
and my exe file is located in
c://myfiles/program/myproject/bin/debug/myexe.exe


So what I want to do is I want to get the existing project location which is only the
c://myfiles/program/myproject
and not the
c://myfiles/program/myproject/bin/debug


I used this code to get the current project location
string startupPath = System.IO.Directory.GetCurrentDirectory()
but it only returns this
c://myfiles/program/myproject/bin/debug
I want to back 2 directories that way I can go to
c://myfiles/program/myproject
...

How do i do that?

Please help.

Thanks in advance
Posted
Updated 11-Oct-11 17:20pm
v2
Comments
Nueman 11-Oct-11 23:20pm    
Edited for punctuation and capitalization and readability.

Do not mix up two completely different directories: working directory (this is the one you found) and executable directory. Working directory is good for simple applications, but it does not depend on where your executable files are, it totally depends on where the users starts the application, and the user can start if from any directory.

In contrast, executable directory has certain location depending where your executable files are. There are several different ways to find it, but some of them are not reliable in that sense that the results depend on how the application is hosted. In particular, running the application under Windows Studio or Service Controller (for Windows Service applications) cause problems. This is the most certain method to find this directory:

C#
string exeDirectory =
    System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetEntryAssembly().Location);


This method also does not depend on the assembly running this code, if finds location of the entry assembly of the currently running application (.EXE file), more exactly, location of its main executable module.

—SA
 
Share this answer
 
Hi,

Try this if could help...

string startupPath = AppDomain.CurrentDomain.BaseDirectory.ToString()



Regards,

Algem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900