Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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?
Posted 6-Sep-12 9:21am
Comments
Zack Mcintosh at 6-Sep-12 15:46pm
   
Also how would you go about finding current user directory?
Sergey Alexandrovich Kryukov at 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.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
C#:
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string executableDirectory = System.IO.Path.GetDirectoryName(location);
VB.NET:
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:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^].
 
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.
 
—SA
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi .
use this cod
 
Application.StartupPath.ToString
 
such as this , on form_load :
 
 MsgBox(Application.StartupPath.ToString, MsgBoxStyle.OkOnly, "Application Directory is")
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^].
 
The current directory, System.Environment.CurrentDirectory is something completely different:
http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx[^].
 
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. Smile | :)
 
—SA
  Permalink  
v4
Comments
Zack Mcintosh at 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 at 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.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
MsgBox(fullPath)
 
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)
 
MsgBox(ns)
 
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.
  Permalink  

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

  Print Answers RSS
0 Richard MacCutchan 335
1 OriginalGriff 330
2 Sergey Alexandrovich Kryukov 259
3 Sharmanuj 146
4 Magic Wonder 129
0 Sergey Alexandrovich Kryukov 5,969
1 OriginalGriff 5,005
2 CPallini 2,473
3 Richard MacCutchan 1,547
4 Abhinav S 1,507


Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 6 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100