Capture only the file path from the file upload control - vb.net

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)

Related

VB.NET open a Text File within the same folder as my Forms without writing Full Path

I found a similar question but it was 5 years 8 months old, had 2 replies and neither worked for me (VB.Net Read txt file from current directory)
My issue is that when I use the following code:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Username_And_Password_Raw.txt")
Dim usernameAndPassword = Split(fileReader, ",")
I get an error saying:
System.IO.FileNotFoundException: 'Could not find file 'C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\bin\Debug\net6.0-windows\Username_And_Password_Raw.txt'.'
I have tried using all the different Applications.BLANKPath options I can find (ie; StartupPath, CommonAppDataPath, etc.) and they all return essentially the same error only with a different location.
This is the folder layout of my TXT File - I know it's a terrible, incredibly insecure and generally awful way of storing login information but this is just for a NEA so will never ever actually be used
This is the actual path of the TXT File if it helps
C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\Username_And_Password_Raw.txt
The startup path is where your exe is located. That and all supporting files get copied to a binary directory when you compile in visual studio, in your case
C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\bin\Debug\net6.0-windows
But what you're trying to do, reference the file where it sits in your solution, is probably not the best way to do it, and your code above will work (with a change, will mention later) if you change the properties of the file in the solution.
Right click on the file in the Solution Explorer Username_And_Password_Raw.txt, select Properties. Modify Copy to Output Directory to either Copy always / Copy if newer, depending on your requirement. Now that file will copy to the same directory your exe is in, and the code above should work.
Note, when creating a path, don't use string concatenation because you may have too many or too few \; use Path.Combine:
Dim filePath = Path.Combine(Application.StartupPath, "Username_And_Password_Raw.txt"
Dim fileContents = My.Computer.FileSystem.ReadAllText(filePath)

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.

File Path of Access Database

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)

Get name of file stored in SQL

I am modifying old application and I have to add functionality to allow user view a file from SQL. File needs to be saved in temporary folder and deleted after closing application. I've managed to do all above, but I have to get filename of downloaded file (now Im using a string variable filename = "TemporaryDoc").
How can I get a filename from a data that is stored in datagridview.Rows(0).Items(0) ?

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")