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

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.

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

How to use Process.Start

I'm using process.Start to run Convert.exe. This program's purpose is to convert all files which are in the exe's folder. So when I normally use it, I copy paste a file into the same folder as Convert.exe and then run Convert.exe. Convert.exe will create a new "converted" file in the same folder.
I'm trying to automate this tedious process. A User selects a file which needs to be converted from FolderA, I copy it to the same folder where Convert.exe is and I'm using process.start(Convert.exe) to run it.
Just to be clear, this "Convert.exe" accepts NO arguments.
The problem: "Convert.exe" is not converting the files in its folder. Instead it's converting all the files in FolderA for some weird reason. I don't know why it picked that folder, I never even try to send it as an argument or nothing.
Here's the code I have:
Dim techInfo As New System.IO.FileInfo(itm.strFilePath)
techInfo.CopyTo(ConverterPath & techInfo.Name)
Dim procInfoConvert As New ProcessStartInfo
procInfoConvert.CreateNoWindow = False
procInfoConvert.Arguments = ""
procInfoConvert.FileName = ConverterPath & "Convert.exe"
Dim procConvert As Process = Process.Start(procInfoConvert)
I did a test where I copy pasted a file into the "Convert.exe" folder and then just run this code:
process.start(ConverterPath & "Convert.exe")
The exe returns nothing, same as if there was no files in the folder.
The only thing I can think of is that when process.Start is run, it copies the file to another location and runs it from there.
Any ideas anyone?
Try this:
procInfoConvert.WorkingDirectory = ConverterPath
That'll set the process up to start in the directory it's contained in, instead of the current directory.