Relative references when form loads in AppData folder - vb.net

I've got a form that can be loaded from USB stick, from my home computer, or from work computers. The form executable is always stored in "some path\National Payroll\Build* and references a database in "some path\National Payroll".
The some path part changes, but the database is always in the parent directory. I've tried other advice to get the form's path, but I can only seem to get the path of the Local AppData folder where the form runs from when it is open, not the path of the form's executable. How do I get that?

Application.StartupPath
And, eventually Application.StartupPath.Parent
EDIT: TEST REPORT
Make a new project. Add a form with two TextBoxes named StartupPath and ExecutablePath. In the Form's Load event put the following code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
StartupPath.Text = Application.StartupPath
ExecutablePath.Text = Application.ExecutablePath
End Sub
Compile and deploy the executable on a USB flash drive (or wherever you want) and double click. This output will be produced:

Related

Exe working only if started manually but I want it to start automatically

I have done a simple VB application with this code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim procName As String = Process.GetCurrentProcess().ProcessName
Dim processes As Process() = Process.GetProcessesByName(procName)
If processes.Length > 1 Then
Process.GetProcessesByName("keyinput")(0).Kill()
End If
End Sub
Public Sub type(ByVal int As Double, str As String)
For Each c As Char In str
SendKeys.Send(c)
System.Threading.Thread.Sleep(int * 1000)
Next
End Sub
Sub vai()
Dim line As String = ""
If File.Exists("trans.txt") Then
Using reader As New StreamReader("trans.txt")
Do While reader.Peek <> -1
line = reader.ReadLine()
type(0.155, line)
'SendKeys.Send(line)
SendKeys.Send("{ENTER}")
Loop
End Using
File.Delete("trans.txt")
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
vai()
End Sub
Basically the timer in it check if a file exists, read it and type the content simulating the keyboard.
I want this exe to start automatically when user login, it does it, apparently, I can see the form1 pop up but doesn't really works. Everyting is fine only if I run it manually by double-clicking the icon. Why and what can I do? Thanks
ps. i already tried to execute it with windows task manager, or putting a shortcut in the windows startup folder, or calling it from a cmd
EDIT:
when app starts automatically , process is running, but windows form is showing like this
Instead starting manually is showing like this:
I don't know this for a fact but I suspect that the issue is the fact that you are not specifying the location of the file. If you provide only the file name then it is assumed to be in the application's current directory. That current directory is often the folder that the EXE is in but it is not always and it can change. DO NOT rely on the current directory being any particular folder. ALWAYS specify the path of a file. If the file is in the program folder then specify that:
Dim filePath = Path.Combine(Application.StartupPath, "trans.txt")
If File.Exists(filePath) Then
Using reader As New StreamReader(filePath)
EDIT:
If you are running the application at startup by adding a shortcut to the user's Startup folder then, just like any other shortcut, you can set the working directory there. If you haven't set the then the current directory will not be the application folder and thus a file identified only by name will not be assumed to be in that folder.
If you are starting the app that way (which you should have told us in the question) then either set the working directory of the shortcut (which is years-old Windows functionality and nothing to do with VB.NET) or do as I already suggested and specify the full path when referring to the file in code. Better yet, do both. As I already said, DO NOT rely on the current directory being any particular folder, with this being a perfect example of why, but it still doesn't hurt to set the current directory anyway if you have the opportunity.
It was a Windows task scheduler fault, that for some reason didn't executed the exe correctly at logon. I've solved the issue by using Task Till Down and everything works fine now.

Open a .exe that is in project folder

The issue I am having is opening a program that's in my project folder after being published. here is my code:
Private Sub B_OpenCruc_Click(sender As Object, e As EventArgs) Handles B_OpenCruc.Click
System.Diagnostics.Process.Start("F:\Deploy\myprogram\Project\myprogram\myprogram\Crucible\Crucible.exe")
End Sub
now it works on my pc but not other pc's. I figured it's because its a full path. I also set properties to "content" and "copy always"
I tried using Dim Path As String = ("\My Project\Crucible\Crucible.exe") I would receive the following error
System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
also this as well:
Severity Code Description Project File Line Suppression State
Warning Assembly 'Crucible\Crucible.exe' is incorrectly specified as a file. SCOfflineLoader
when I publish it.. the files are there but my program can't open it.
I can't seem to figure this out, is there a solution to this?
Basically the Goal I want to achieve is When I click a Button It will open a .exe
OR
When I click a button It will Open a .exe that the Target user already has (i.e notepad.exe)
Thanks.
You can use the Application.StartupPath property to get the directory where your application is running from.
Dim path As String = Path.Combine(Application.StartupPath, "Crucible.exe")
System.Diagnostics.Process.Start(path)

vb.net Process.Start local path

So i know in html you can use ../xx/ to open files in the same folder as the code. I want to do the same with Process.Start. This is my attempt at is but it cannot find the file.
Private Sub btn_database_Click(sender As Object, e As EventArgs) Handles btn_database.Click
Process.Start(".\WindowsApp10\WindowsApp10\WindowsApp10\WindowsApp10\bin\Debug\InventoryManager2.accdb")
End Sub
This is the path file I'm trying to open. C:\Users\Michal\Downloads\WindowsApp10\WindowsApp10\WindowsApp10\WindowsApp10\bin\Debug\InventoryManager2.accdb
Thanks in advance
Application.StartupPath will give you the path of the folder the current EXE was run from, so:
Process.Start(IO.Path.Combine(Application.StartupPath, "InventoryManager2.accdb"))

select files from setup location vb

I work on my VB application on visual studio 2012
I created a button, On clicking it, it plays a file on my PC ("D:\My Project\Sound_01.wav")
so the code will be like that:
**Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
My.Computer.Audio.Play("D:\My Project\Sound_01.wav")
End Sub**
My problem is that I need to make setup file for that app (using install shield 2015) and the file location will return error because the destination PC may not have the same location("D:\My Project\Sound_01.wav")
can anyone advise me how to do that?
add the file to your application path so you can use following:
My.Computer.Audio.Play(Application.StartupPath & "\Sound_01.wav")

Cannot set OpenFileDialog initial directory to Downloads in visual basic [duplicate]

This question already has an answer here:
How to reference the current Windows user's video folder path in VB.net
(1 answer)
Closed 6 years ago.
I am trying to set the initial directory to the downloads folder but it doesn't work, Even though it's a perfectly valid path. Here is the code that I am using:
Private Sub btn_AddMod_Click(sender As Object, e As EventArgs) Handles btn_AddMod.Click 'This brings up the file dailoge
Dim Downloads As String = "\Downloads" 'A variables called \Downloads
Dim UserprofilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) 'This finds the directory to the User profile environment variable
Dim Downloadspath As String = UserprofilePath.Insert(0, "") + Downloads 'This adds \downloads to userpath
OpenFileDialog1.InitialDirectory = Downloadspath 'This sets the Open File Dialog to go to the users downloads
txt_setmodname.Text = Downloadspath 'This is used for debugging, it sets a textbox to the path
OpenFileDialog1.ShowDialog() 'This opens the Dialog
End Sub
When I copy the output text, the path is perfectly valid but instead of taking me to the path, it takes me to MyDocuments
That's some wacky code you have there. I'm not sure why it doesn't work and I'm not too interested in finding out. I just tested this and it worked as you want:
Using ofd As New OpenFileDialog
ofd.InitialDirectory = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads")
ofd.ShowDialog()
End Using
Obviously you can use an OpenFielDialog that you created in the designer instead of created in code if you want.
By the way, it should be noted that the user's Downloads folder is not necessarily in that location. Mine is on my D: drive, while my personal folder is on my C: drive. For people who keep C: only for system files, all their libraries and the like may be on a secondary drive. Unfortunately, there's no easy way to get the path for the Downloads folder like there is for Documents and some others. I'm guessing that the path is stored in the Registry or the like, but I'm not sure where.
I looked further into it and found out that there is a registry entry for the downloads path, so I used that instead and that seemed to have worked, My code is as follows.
Private Sub btn_AddMod_Click(sender As Object, e As EventArgs) Handles btn_AddMod.Click
Using ofd As New OpenFileDialog
Dim DownloadsPath = My.Computer.Registry.GetValue(
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\", "{374DE290-123F-4565-9164-39C4925E467B}", Nothing)
ofd.InitialDirectory = DownloadsPath
ofd.ShowDialog()
End Using
I'm not sure why the other method didn't work, it always took me to the MyDocuments folder for some reason.