Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

this folder 2

Status
Not open for further replies.

seanbo

Programmer
Jun 6, 2003
407
0
0
GB
i am writing a program that laod some bmp files from a folder called images, located in the same folder as my code. at the moment i load them using code a bit like this:

aPicture = Image.FromFile("Images/picture.bmp");

the program now (almost) has the ability to load by double clicking any file associated with it. the problem is, wheni do that the current directory (System.Environment.CurrentDirectory) is where the save file is, not the code. how can i find what directory the code is in?

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
I am not sure to understand which folder you are looking but I guess you are asking for the assembly path:
Code:
// Retrieve assembly path 
private void MyForm_Load(object sender, System.EventArgs e)
{
//...   
string	assemblyPath = Assembly.GetExecutingAssembly().Location;
int pos = assemblyPath.LastIndexOf('\\');
if(pos == -1)
	return ;
int len = assemblyPath.Length;
assemblyPath = assemblyPath.Remove(pos + 1, len - pos -1);
//...
}
For the @"F:\Visual Studio Projects\MyProject\bin\Debug\myapp.exe"
the above lines will give "F:\Visual Studio Projects\MyProject\bin\Debug\"

-obislavu-
 
Not a nice one but a workaround could be to store your path in the registry on first run and the read the path from there.

Stephan
 
You could also do:
Code:
string assemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top