Solved

How do I refer to the Application folder in VB code?

Posted on 2004-04-18
8
1,260 Views
Last Modified: 2010-08-05
This is my first vb.net app, using Visual Studio .Net 2003.  My app uses an Access database.  It installs and runs fine, provided the end-user doesn't change the default install path.

The installation package puts a copy of the .mdb file in the Application Folder.  To open this database, I use the following connection string in my code:

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"Data source=C:\Program Files\CompanyName\Appname\Access.mdb"

If the user chooses any other install path, the Access database is placed there, and the application cannot find it.

How do I change that connection string to refer generically to "Application Folder" so it will find the database no matter what install path the user picks?

Thanks
0
Comment
Question by:sheer314
8 Comments
 
LVL 15

Accepted Solution

by:
Timbo87 earned 100 total points
ID: 10855511
If you just type in the name of the database like this:

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"Data source=Access.mdb"

It will automatically use the application directory. If you absolutely need the full path (don't know why you would), you can get it like this.

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"Data source=" & Environment.CurrentDirectory() & "\Access.mdb"
0
 

Author Comment

by:sheer314
ID: 10856271
Thanks.  If I specify it that way, and try to run the app in debug mode, I get an error:  "C:\Documents and Settings\My Name\My Documents\Visual Studio Projects\AppName\bin\Access.mdb" is not a valid path.

If I put a copy of the database in the bin folder, then it will run in debug mode, and it also fixes my install problem.  

So that works, but I'm curious why I need the extra copy in the bin folder, since I've already put it in the Application Folder, which is where it goes on installation.  Shouldn't the debug mode be able to find it there?
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 10856336
"If I specify it that way"
Which way?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Assisted Solution

by:graye
graye earned 50 total points
ID: 10856385
No, while running the debuger the program *is* running from the debug folder.

I typically use a "Data Source=../database.mdb" while debugging, and then switch it back when I'm ready to deploy the application.

Another idea is to do something like this:

#if DEBUG then
blah;"Data Source=../database.mdb"
#else
blah;"Data Source=database.mdb"
#endif

Yet timbo87's suggestion of using the CurrentDirectory is a bit more slick
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 10856407
Graye's idea with preprocessor directives is a great way to do it, too. When you run in debug it's looking for the file in project\bin\Debug and when you run in release it's looking for it in project\bin\Release.
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 10856469
Try this function:

'Get Application Path
    Public Function getAppPath(Optional ByVal removeSeparator As Boolean = False)
        Dim tmp As String = System.Reflection.Assembly.GetExecutingAssembly.Location.ToString
        tmp = Replace(tmp, Dir(tmp), "", , , CompareMethod.Text)
        If removeSeparator And Right(tmp, 1) = "\" Then tmp = Left(tmp, Len(tmp) - 1)
        Return tmp
    End Function

use like :

conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"Data source=" & getAppPath & "Access.mdb"

Hope this helps
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 10856975
VB6 App.Path Method...Is there a .Net Equivalent:

Application.StartupPath
System.Reflection.Assembly.GetExecutingAssembly.Location
System.Windows.Forms.Application.ExecutablePath
System.Windows.Forms.Application.StartupPath
System.AppDomain.CurrentDomain.BaseDirectory()
System.GetEntryAssembly().Location
0
 

Author Comment

by:sheer314
ID: 10857067
Great answers, thanks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question