Solved

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

Posted on 2004-04-18
8
1,258 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 50

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 48
VB.NET 2008 Populate DataModel with DataTable 44 38
How to retrieve a mysql date column that has 0000-00-00 in vb.net 8 42
vb.net and creating a class 5 20
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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