Solved

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

Posted on 2004-04-18
8
1,255 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
Comment Utility
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
Comment Utility
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
Comment Utility
"If I specify it that way"
Which way?
0
 
LVL 41

Assisted Solution

by:graye
graye earned 50 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:Timbo87
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
Comment Utility
Great answers, thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now