File Path of Access Database - vb.net

I'm working with vb.net 2008 as well. But I have a question.How to remove a file path like this C:\users\myDocu\debug\Dbase.accdb and I only want is the file name Dbase.accdb. Because I want to transfer my files in another computer but the problem is the file path. I always need to change the entire location in my codes to run without debug.

To get the filename without the path, you can use Path.GetFileName.
But if you want a painless way to find a place to store your database, consider putting it into the application data folder (AppData). You can get this folder with Environment.GetFolderPath and Environment.SpecialFolder.ApplicationData, using it like this:
Dim pathToDb = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Dbase.accdb")
if you want to use the file locally. If you want to share the file between different instances of your application in a network, put the path e.g. in a config file like App.Config.

Try this:
Dim FullFilePath As String
Dim FileName As String
FullFilePath = "C:\users\myDocu\debug\Dbase.accdb"
FileName = Mid(FullFilePath,InStrRev(FullFilePath,"\") + 1)

Related

Grabbing current user.config path (Visual Basic)

Is there a way of grabbing the location of the My.Settings user.config location? So for example I want to be able in VB to grab the path of the user.cofing file path to a string
The reason I ask is that I have an application where the user.config file is backed up and then restored, the issue is that with the my.settings folder structure it uses a unique hash with the folder name meaning that I cannot write into the code a static folder path, instead I need to be able to grab the location of the user.config OR be able to get the folder name of the application AppData.
Any ideas?
To put this in perspective, currently I'm using something like this:
Dim filePath As String
filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\My_App\My_App.exe_Url_<the_hash_that_changes_causing_issues>\1.0.0.0\user.config"
Because of the hash change this will not always work
Try executing this code:
Dim mainConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim userConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming)
Dim userLocalConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
Console.WriteLine(mainConfig.FilePath)
Console.WriteLine(userConfig.FilePath)
Console.WriteLine(userLocalConfig.FilePath)
You'll need to reference System.Configuration.dll and import System.Configuration.

Visual Basic: File is said to be in the wrong folder

Ok, here is my story:
I am building a fileviewer, and i am trying to delete the selected file in the listview.
when i try to delete it, it gave me an error saying the file wasnt found. I looked at my desktop and the file was there. here is the original code:
dim f as string = lv1.focuseditem.text
my.computer.filesystem.deletfile(f)
lv1.update()
this gave me that error. My updated code is supposed to show me where the computer thinks my file is:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
this shows a messagebox that shows the path of:
C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt
but the real file location is:
C:\Users\tgs266\Desktop\test.txt
please help
How are you getting the filenames for the ListView? Is it just the filename and no path?
If, for example, lv1.FocusedItem.Text is "test.txt", and that is the value you use (without the path), by default the program will look in the directory it's executing in. This is most likely why you're seeing C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt as the location, instead of what you expected.
If all the files are on your desktop, you can use Environment.GetFolderPath in conjunction with the Environment.SpecialFolder Enumeration to get the file, like this:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
However, if you're going to have files scattered throughout your system, you'd be better off storing the full path as #Plutonix indicates in his comment.
It looks like your code is looking in your applications path on the server while you want to look at the users desktop location.

Reference a file in Unit Test (VB.Net 2012)

I'm really puzzled on this one.
I an writing unit tests for a project. I need to open and use a file for the testing. I am developing in two environments (work and home) and am using Dropbox to keep my code to be able to use the most recent version at either place.
I have created the test file and have placed it in a folder in the Unit Test project. I could obviously use the file spec
c:\blah\blah\blah\file.txt
but that would only work on one machine.
So my question is...
Is there a value/parameter/Constant that I could use in my test code that would have the root of the project regardless of which location I am at. I want to be able to reference the file like so:
{projectRoot}\folder\file.txt
You can get the path to the folder in which the current executable resides, like this:
Dim exeFolderPath As String = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
Or:
Dim exeFolderPath As String = Path.GetDirectoryName(Environment.GetCommandLineArgs()(0))
Or, if you are in a DLL, and you need to get the path to the folder containing that DLL, instead of the executable that is using it, you can do this:
Dim assemblyFolderPath As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Alternatively, you can just use the current folder, like this:
Dim currentFolderPath As String = Directory.GetCurrentDirectory()
Then you can drill down to a sub-folder, like this:
Dim subFolderPath = Path.Combine(exeFolderPath, subFolderName)
In order to make it so that the text file, which is included in your project, exists in the output binary folder, just select the file in the Solution Explorer, go to the properties window and change the Copy to Output Directory setting to either Copy always or Copy if newer. Then, when you build your project, the file will automatically get copied to the appropriate bin folder.

Renaming a file in %appdata% in VB.net

So I need somone to tell me how to fix this code. I'm trying to rename a file which is in C:\%appdata%\Roaming\.minecraft\bin\XenonUpdate.jar to minecraft.jar.
The code I am using is:
My.Computer.FileSystem.RenameFile("C:\%appdata%\Roaming\.minecraft\bin\XenonUpdate.jar", "minecraft.jar")
Can someone fix this?
%appdata% not not a valid path, rather it denotes a special folder that you can get by using Environment.GetFolderPath, once a get the %appdata% path, you can easily rename file.
Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim file_to_rename = Path.Combine(folder, ".minecraft\bin\XenonUpdate.jar")
My.Computer.FileSystem.RenameFile(file_to_rename, "minecraft.jar")
File handling functions do not deal with environment variable expansion, %appdata%. You need to do this yourself.
My VB.Net is non-existent, but I think it would look like
Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim from = path + "\.minecraft..."
Dim to = path + "\.minecraft..."
My.Computer.FileSystem.RenameFile(from, to)
Also, see C# getting the path of %AppData%

Capture only the file path from the file upload control

I am trying to update some local access databases from a web database. When I am accessing the web page, I want the user to select a folder on the local hard drive where the DB's reside that will be updated by the program. I actually have been using the File Upload control to do this as it has allowed me to check the file extensions on the local DB's their file size and other misc. items.
This captures the full path with the file name...
fileName = FileUploadAccess.PostedFile.FileName.ToLower
Now I have a need to capture the full path of the db's on the local hard drive. I can get the full path with the filename, but then I need to write a bunch of code to strip the filename from the path variable.
Is there a simple way to capture the path of the location the DB when the file location is selected, something like this....
Dim thePath As String = System.IO.Path.GetFullPath
Thanks for any suggestions.
You can use IO.Path.GetDirectoryName
Here is a sample code:
Dim MyDirectoryPath As String = System.IO.Path.GetDirectoryName(MyFullPath)