How can I get access to the files root directory without giving full path name in VB .NET - vb.net

I am developing a Windows application in Visual Studio 2008 with SQL Server CE as database. D:/My Project/Library is the root directory for my project's source code.
I have some files which I want to access within the forms of my project. They are stored in D:/My Project/Library/New Resources. To access those files I can't use the full path name because I keep changing the location of my project's source code. I need something like wherever the source directory may be the files must be accessed from there only.
Ex:
Public Sub setconnection()
con = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=H:\Library\Library\Library1.mdf;Integrated Security=True;User Instance=True")
cmd.Connection = con
con.Open()
End Sub
I want to use relative path name for Library1.mdf file without using H:\Library\Library path name.

This has worked for me, though you may need to tack on some additional info. A web project using this, for example, may need to append "/bin" to the end of the result to get the actual location of the DLLs.
AppDomain.CurrentDomain.BaseDirectory

Related

How do I get the Base Directory of my Project (The path where the .vbproj or .sln file are located)

I'm trying to get the Base path of my VB Project dynamically since the path that the project is run from will change.
Let's say that the VB Project file is in C:\MyVBProjects\May2016\MyProject\MyApp.vbproj
I tried using Application.ExecutablePath, Assembly.GetExecutingAssembly().Location and Application.StartupPath
The above three return C:\MyVBProjects\May2016\MyProject\bin\Debug\
I also tried using the following code:
Curr = Directory.GetCurrentDirectory()
Root = Directory.GetRootDirectory(Curr)
This returns just C:\
Is there anyway I can get it to say C:\MyVBProjects\May2016\MyProject\ ?
I'm currently using Visual Basic 2012 on Windows 7.
Works in design time :
Dim DTE As EnvDTE.DTE = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE")
Dim Prj As Project = DTE.Solution.Projects.Item(1)
Dim PrjFile As String = Prj.FullName
You should reference "envdte" (in Assemblys -> extensions)
and imports envDTE.

Get a VB 2008 file path relative to the published executable location

I have a Visual Basic Studio 2008 project that I need to deploy in 3 separate server environments. Each of those environments has a different filepath for file storage, but other than that the execution of the programs will be exactly the same in all environments.
In order to accomplish this, I'd like to instruct the program to look at a text file in the same folder as itself for the file storage file path for its environment; then i can just clone the same VB program 3 times and change the contents of the text files whenever the storage locations change.
Before publishing an executable file, I can store the text file in the bin->debug folder and use any of the VB relative path methods that I've come across (App.Path, System.IO.getcurrentdirectory, My.Application.info.directoryPath, etc) to access it with no problem. When I publish the project, however, these find the relative path of the program as buried deep within the installed user's appdata. I want to access the text file on the server where the user goes to run the executable.
So my question is: how can i get the filepath of the published executable location? I have searched for 4 hours and have been unsuccessful in finding an answer.
Relevant code:
dim fso as new scripting.filesystemobject
dim ts as scripting.textStream
ts = fso.opentextfile(My.Application.Info.DirectoryPath & "\HostFiles\rootDir.txt")
rootDir = ts.ReadAll
topDir = rootDir & 'rest of file storage location
Maybe you could try this if you are using VB.Net:
Application.StartupPath
Eg:
Dim strContent As String = IO.File.ReadAllText(Application.StartupPath & "\HostFiles\rootDir.txt")

UNC File Path is not working as connection string for an access database located on a network drive

I am attempting to insert new records into an access 2010 database that is located on a network drive with a windows form app developed in Visual Studio 2010 using vb.net. When using the mapped drive letter in the path the connection string works properly, but when using the UNC path I receive an error telling me that it "is not a valid path".
The working path looks like this...
Dim dbSource = "Data Source= L:\server\directory\file.accdb"
The path that errors looks like this...
Dim dbSource = "Data Source= \\server\directory\file.accdb"
Is there something basic I'm missing? The only difference is the use of the UNC path.
As a secondary question, would there be any issue to just use the mapped drive letter if I anticipate all users would have the server mapped to the same drive letter?

What is proper way to define connection string VB

My Visual Basic project uses this connecton string:
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Admin\Desktop\IzolacCold V2\IzolacCold V2\izolac_cold_dbv2.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
When I debug it, and go to debug folder, or when I "Release" it then problem occurs. If I run exe file program works normally but it save's data in database defined in connection string and not in database that is created in DEBUG folder (or RELESE folder).
How to properly connect to my databes. How to build proper connection string ?
You should use the DataDirectory substitution string.
For example you could write your connectionstring as
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\izolac_cold_dbv2.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
In your code then you could control the exact location of the DataDirectory executing this code, BEFORE any attempt to open a connection.
Dim commonDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Dim myAppDataFolder = Path.Combine(commonDataFolder, "MyAppName")
if Not Directory.Exists(myAppDataFolder) Directory.CreateDirectory(myAppDataFolder)
AppDomain.CurrentDomain.SetData("DataDirectory", myAppDataFolder)
(This example use the C:\programdata\myAppName folder because it is the most appropriate location for read/write data files needed by your application
Your deployment procedure should take care to create the above folder and place the database file in this location.

Connection String for a local database across multiple users

How do you use the connection string on a local database in a way that would allow you to transfer the project folder from one computer to another, without having to modify the connection string?
Not like this
connection_String As String = "Data Source=C:\Users\Kyle\Desktop\CSCI_388_Group_Project\CSCI_388_Group_Project\CSCI_388_Group_Project\CSCI_388_Group_Project_Database.sdf"
More like this
connection_String As String = "Data Source=|DataDirectory|\CSCI_388_Group_Project_Database.sdf"
If I try to enter the second example into the connection field under the database properties I get an illegal character error.
The Environment class contains most of the paths you'll need.
You can call Environment.GetFolderPath to get a special directory. For example, to get the My Documents directory, call Environment.GetFolderPath(Environment.SpecialFolder.Personal).
You can browse all of the special folders here:
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
In your example specifically, you'd use the following:
connection_String As String = _
"Data Source=" &
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"CSCI_388_Group_Project_Database.sdf" )
Alternative:
Is it possible to just ask the user? If it's on their desktop, they might move it.
If you decide to keep the sdf file in the same folder in which your exe would reside, then you can do like this.
Dim ExeFolder As String = AppDomain.CurrentDomain.BaseDirectory
Dim connection_String As String = "Data Source=" & ExeFolder & "CSCI_388_Group_Project_Database.sdf"
Hope it helps !
One simple way is to use an environment variable to specify your path to the folder.
Then in your VB program using the Environ function to get the environment variable settings and use string manipulation to build your connect string.
You will need to have a default in case the environment variable is not set.
However when you launch your application, you can do it within a script that will set the environment variable.
A second method is to specify the path name on the application command line so that when the application is launched, it will specify the path using the command line.
A third method is to have a Windows registry key that you use. Then, similar to the environment variable, you query the Windows Registry for the folder path, then build your connect string from that.
With this third method you can have your application installer insert the Registry key.
A fourth method is to have an .ini file that contains an entry for the folder path.