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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to reference a file? 1

Status
Not open for further replies.

crtillerson

Programmer
Nov 4, 2003
8
0
0
US
I include a .mdb in my application which is used as a lookup. I have a variable that holds the relative path and filename which looks something like this:
Dim DSN As String = "..\MDB\Lookup.mdb"

In Visual Studio .Net when I build and run the program it works. When I deploy the program the relative path no longer works, regardless where I put the executable, under the application root directory or if I simulate the structure in the dev environment and put it in a directory called "bin". I've tried using Application.StartupPath, but this does not work in the dev environment because the .exe is stored in "bin".

What is the best way to reference file that is packaged with the application?

Thanks for any input.

Clint
 
Are you trying to set it up so that you have a directory of databases below the app's startup path? If so, just use the application startup path, and remember it's a string... it can be concatenated.

Application.StartupPath & "\MDB\Lookup.mdb"

Actually, I can't remember right off-hand whether StartupPath includes the "\" or not... so if this doesn't work exactly as shown, change it to "MDB\Lookup.mdb".

You were on the right track, I think... unless you're 100% sure of this file's absolute path, it's best to use the StartupPath.

Ben
A programmer was drowning. Lots of people watched but did nothing. They couldn't understand why he yelled "F1!"
 
Hi Clint
You should have a sub-directory named "MDB" with that .mdb resides in it under your project and you are done
"..\MDB\My.mdb" should search the file in \MDB\My.mdb
you don't have to do anything special if that directory is included in your project or present then you don't have to do anything
Regards
Nouman

Nouman Zaheer
Software Engineer
MSR
 
Thanks for your replies. What you say makes perfect sense to me, but here's where things are going sour which leads me to believe I'm misunderstanding something.

When I test/debug the program by clicking on the build button in Visual Studio, the executable is placed in a folder called "bin" beneath the working application directory. So, in this case the relative path reference works, "..\MDB\Lookup.mdb".

However, when I build the release version and deploy the application, I want to distribute the application with the .exe in the application folder, rather than in some subdirectory beneath the application folder. So, my relative path no longer works. On the flip side, if I use Application.StartupPath, an error is thrown when I debug because the startup path is actually "application folder/bin".

I'm wondering how others handle this or is this a problem for anyone else. Where do you normally stpput your executable.

Thanks again. I hope this clarifies the nature of my problem. I'm new to VB and .Net so my knowledge is very limited.

Clint

 
I have this line of code in the declarations section of one of my major applications:
[blue]
Code:
Private ExecutablePath As String = Application.StartupPath & "\"
[/blue]
Then, when I need a file in the same directory as my application, I do this:
[blue]
Code:
ExecutablePath & "myFile.ext"
[/blue]

I think the answer is in the way you set up your development environment. You should make sure that any subfolders containing data are in the \bin folder... like this:

\bin
+ \MDB
+ \Images
...

If you have them all on the same level, then you're right, you can't do that from the startup path. But if you have a folder whose path is X:\...\bin\MDB, then you'll be able to find your files... and when you set up the installer, that's how it's going to add these other folders.

"\bin" becomes the folder where the .exe will reside. Then any directory under "\bin" will go deeper than the app's startup path.

Ben
A programmer was drowning. Lots of people watched but did nothing. They couldn't understand why he yelled "F1!"
 
Thanks Ben! That works. My only deviation from this is when I set up the deployment project, I put the .exe at the application root and \MDB just beneath and it works fine since I'm concatenating "\MDB\Lookup.mdb" to StartupPath.

Thanks for your help!!

Clint
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top