[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-04-18
8
Medium Priority
?
1,266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 15

Accepted Solution

by:
Timbo87 earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 41

Assisted Solution

by:graye
graye earned 200 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 53

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

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!

Question has a verified solution.

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

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 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