How to open a file from folder where EXE was opened. VB - vb.net

Part of a program I am making I need to open a file (for example a txt file) from the folder where the program was opened.
The idea is that it can be zipped up and put anywhere without having to place the file in a certain location.
It's got to be Visual Basic and I will really appreciate some help.
I have googled this but found nothing for VB. I'm relatively new to the language.
Thanks, Jack

To open the file do this:
Dim fileName as String = "yourfile.txt"
Dim appDir as String = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Process.Start(appDir & "\" & fileName)

You can use this to get the path to the folder where the currently executing assembly (i.e. the EXE) is located:
System.Reflection.Assembly.GetExecutingAssembly().Location.Substring(0, assembly.Location.LastIndexOf(System.IO.Path.DirectorySeparatorChar))

Related

Missing txt file from Visual Basic

I am working in Visual Basic 2017. I have tried to add the file to the Debug folder, but then it just shows that the txt file ienter image description heres missing. I don't have the option under the "Word Solution".. How can I make the file show up? It keeps telling me it doesn't exist.
Dim inFile As IO.StreamReader
Const FileName As String = "words.txt"
Dim subscript As Integer
You can get the path of the directory (Debug or Release or any other) of the *.exe file with:
Dim directory as String = My.Application.Info.DirectoryPath
Using this information, you can then construct the full path with
Dim path As String = IO.Path.Combine(directory, FileName)
If IO.File.Exists(path) Then
...
You can check in Windows File Explorer to see where the file actually is (notice the Copy Path on the ribbon). In File Explorer you will see that the .exe you are running is down 2 directories from the Words Project directory. The double dots in the path is an old DOS way to navigating around directories without having to type out the whole path. This tells the compiler to find the file up 2 directories from the current directory.
For testing purposes this will work. For a release version you could add the file to Resources and access it the same way in any version.
You don't need a stream for a text file. .ReadAllLines returns an array of the lines in the text file
Private Sub OpCode()
Dim words = File.ReadAllLines("..\..\words.txt")
End Sub

During design/debug process, where do files created in LocalAppData directory go?

I feel really stupid for having to ask this (I'm not a professional): during the development/design/debugging process, where do files created in the LocalAppData go? I haven't yet finished my project, so it hasn't yet been installed on any machines. Parts of my code are supposed to write information to a text file in the LocalAppData directory. However, when I run my program for testing/debugging, the text files are nowhere to be found.
Dim dir As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) & "\MyFolder\"
Dim path As String = dir & DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") & ".txt"
If Not Directory.Exists(dir) Then
Directory.CreateDirectory(dir)
End If
Using writer As New StreamWriter(path, True)
writer.WriteLine(message)
writer.Close()
End Using
Are these files being created somewhere else? Are there special dummy folders hidden somewhere for development purposes? I tried changing the directory to Desktop and it worked. What gives?

How to Auto Copy text file from one folder to another folder in vb 2010 console or windows application

I want to create a program that auto copy text file from one folder to another folder . is it possible to make in windows form in vb.net ? if not what about in console apps ? i tried to search but i didn't find an answer for both. please help me i'm new to to this. I want to copy all the text file that is being save to c:folder1\test1.text copy to c:folder2\test1.text then test2.text,test3.text all the text file that are being put in folder1. i want to copy in folder2.
now i only have this code:
it will only copy 1 specific textfile with file name test.txt.
enter code here
My.Computer.FileSystem.CopyFile("C:\CopyTo\test.txt",
"C:\CopyHere\test.txt")
Of course! First of all we need a function that search for files.
Public Sub SearchFiles(ByVal Pattern As String, ByVal Path As String, ByVal FilesFound As ArrayList)
FilesFound.AddRange(Directory.GetFiles(Path, Pattern))
End Sub
But where we should save the list of files? We can use a Array for it. Also we should define our output and input folder
Dim files As New ArrayList
Dim inDir As String = "input path"
Dim outDir As String = "output path"
We can now call this function like this:
SearchFiles("*.txt", inDir, files)
All .txt files in the folder are now saved in our Array List. But how we can work with it? We can now work with it like this:
Try
For Each file As String In files
Dim fName As String = Path.GetFileName(file)
My.Computer.FileSystem.CopyFile(file , outDir & "\" & fName, overwrite:=False)
Next
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
This will copy every .txt file that where found in our inDir to our outDir. If something goes wrong then you will see this in the console. Try it out and understand how it works :)

VB.NET - How to open files of a given file type?

I want a quick way to open a bunch of Visual Studio projects at once. I have a collection of folders, and I want to run some code that runs through each subfolder and opens the file that ends with ".sln". In pseudocode:
For each SubFolder in ProjectsFolder
Open SubFolder
Start "\*.sln"
Next
Might be I've missed something obvious, but I couldn't find anything helpful through Google.
Try using the Process class:
Dim parentFolder As New DirectoryInfo("c:\myfolder")
For Each f As FileInfo In parentFolder.GetFiles("*.sln", _
SearchOption.AllDirectories)
Process.Start(f.FullName)
Next

Download to root directory

Okay, so I have been searching for ages to find this but no luck.
I am using:
Me.downloader.DownloadFileAsync(New Uri(fileUrl), Path.GetFileName(fileUrl), Stopwatch.StartNew)
To download a file but I want it to save to the root directory of my program in a file called launcher.
So for example, if my program is on the desktop and I open it and click start I want it to create the launcher folder if it's missing then download the files into that and if it's not then just download the files into it.
I've been looking everywhere to find code which would allow me to do this and I have tried lots of different things.
At the moment, it justs saves in the root directory of where the program is.
Thanks.
Try something like this:
Dim baseDir As String = AppDomain.CurrentDomain.BaseDirectory
Dim launcherDir As String = Path.Combine(baseDir, "Launcher")
If Not Directory.Exists(launcherDir) Then
Directory.CreateDirectory(launcherDir)
End If
Dim targetFile = Path.Combine(launcherDir, Path.GetFileName(fileUrl))
Me.downloader.DownloadFileAsync(New Uri(fileUrl), targetFile, Stopwatch.StartNew)