Solved

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

Posted on 2004-04-18
8
1,257 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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