Click here to Skip to main content
15,749,352 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I would like to know how to find the directory that a program is in? Like say it is on the desktop. How do I get the program itself to find the directory that it is in and set a string or setting as the directory?
Zack Mcintosh 6-Sep-12 15:46pm    
Also how would you go about finding current user directory?
Sergey Alexandrovich Kryukov 6-Sep-12 16:14pm    
What is that? There is a "current directory" and some user-related directories specified in the user profiles (so called "special folder").
Anyway, see my Solution 2.

This is one of the methods which does not require any specialized assembly references (such as the method using System.Windows.Forms.Application) and always works correctly, no matter how application is hosted:

string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string executableDirectory = System.IO.Path.GetDirectoryName(location);

Dim location As String = System.Reflection.Assembly.GetEntryAssembly.Location
Dim executableDirectory As String = System.IO.Path.GetDirectoryName(location)

What it does is: it finds the location of the main executable module of the entry assembly, the one containing your application entry point ("Main"). Of course, you can find location of any other assembly, for example, the calling assembly. Please see:[^].

It's important to understand that there are some different ways of finding this directory, but some of them depends on how application is hosted (my not return correct results in case of Windows service or when are executed under the debugger), but the method I show here always works correctly.

Share this answer
Hi .
use this cod


such as this , on form_load :

MsgBox(Application.StartupPath.ToString, MsgBoxStyle.OkOnly, "Application Directory is")
Share this answer
Answering the additional question asked by OP in the comment to the question:

The user have directories associated with their profiles; and also there is a similar directory for "all users". This is all called "special directories" ("folders"). Get them using System.Environment.GetFolderPath:[^],[^].

The current directory, System.Environment.CurrentDirectory is something completely different:[^].

It's important to understand that you should never rely on any particular current directory even it your application never change this property value. It entirely depends on how the user starts the application. It can be absolutely anything. By default, it simply depends on the current directory of the parent process (quite typically, some file manager, such as Windows Explorer) and also on the parameter of a .LNK file is one is used. That's why this property is not often used in good software. It's more typical that an application (especially a console application) uses the files from the current directory, whatever it is, but in a way agnostic to what is that directory, simply by using relative path names supplied by user (in a command line or whatever).

I think, now you are pretty well armed with the knowledge on directories relevant to your application during run time. :-)

Share this answer
Zack Mcintosh 7-Sep-12 1:18am    
Just to fill you in so you can help even more. This is to work with a program called DropBox. It creates it's directory in the user's folder that installs it. I want to make a program that will look for, find, and load the file in the user's folder. Not just my folder though but on someone else's computer as well so it has to find the current user or whatever to be able to load the .txt files into a textbox so I can edit them straigt from a program instead of having to keep opening them. Would I be able to place the program in that folder and have it return the directory of that folder with the code above? That makes since to me at least.
Sergey Alexandrovich Kryukov 7-Sep-12 15:05pm    
I'm sure I already told you all you need for this. You need to use GetFolderPath for all cases you've mentioned.

Now, it's not clear what do you mean by "someone else's computer". For a system, this is just the same. If a user logs on on some computer for the very first time, it uses its local account or, say, a corporate domain account, this user because just another use, as well as that "someone" person. In fist case, the associated local account data (and all those directories) are pre-created when the user acting as an administrator adds a local account; in second case, all the data and sub-directory structure associated with the domain account is created when the user logs for the very first time.

I hope I answered. Please consider accepting this answer formally as well.
Your follow-up questions are welcome, of course.
Sergey has some good links, you should review them.

Another option for finding the path of your programs file is to use the "My" Namespace
Like this to get the path to the Program for where it was started from.

Dim CurrentLocation As String
CurrentLocation = My.Computer.FileSystem.CurrentDirectory

As far as the Current User Directory.

There is no Special Folder in System.Environment.SpecialFolder for the C:\Users\[Current User] in .Net 3.5
I generally did something like this

Dim CurUser As String
CurUser = System.Environment.UserName
Dim fullPath As String = "C:\Users\" & CurUser

There are two problems with this way that I can think of at the moment.
If the Username was changed then it will not match the name that is mapped to the folder It will be the name that the account was originally created with.
In my test case the original name would be something like "B", When it should have been "Bill" So the folder maps to "B" and the user name is "Bill".
If you use this on Vista or higher it will work. If you try and use it on XP then it will fail because of the path difference in XP and Vista.You would also need to add code to detect what OS you are on to Use the Correct Path For the Correct OS.

In order to get around the those problems of the username changed and OS Version I just came up with this.(watch the word wrap here)

Dim userpath As String
userpath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim trimmed() As String
trimmed = userpath.Split("\")

'create a new string with just the first parts leaving off the last backslash and the documents folder
Dim ns As String
ns = trimmed(0) & "\" & trimmed(1) & "\" & trimmed(2)


This code works on a (Vista) system that had the Username changed.
I also tested on a XP virtual machine and it works there too.
I hope this helps.
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