vb.net Process.Start local path - vb.net

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

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.

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

Relative references when form loads in AppData folder

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:

Getting "Access Denied" while trying to save executable files on C drive [duplicate]

Hi I checked All Questions Present here did not helped me so I asked
Ok here we go
I try to copy file from "Resources" to "C:\Test" Folder does not work for me
here what i tried:
Firstly, I put test.txt file in my resources to copy to "C:\Test" folder but I get an error
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
IO.File.WriteAllBytes("C:\Test", My.Resources.Test)
End Sub
End Class
i get error i cant even debug
ERROR :- Error 1 Value of type 'String' cannot be converted to '1-dimensional array of Byte'.
So is there any way I can copy .txt file to "C:\Test" folder if it exist so that I can copy replace it ?
Now I changed the extension of test.txt to test.bin
It allows me to start the program in debug and no error with this code;
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
IO.File.WriteAllBytes("C:\Test", My.Resources.Test)
End Sub
End Class
Now when i click button1 i get error
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'C:\Test' is denied.
So i want to copy the file and replace if it is present there suppose file is test.txt and test.bin both cases
now what code will work so that i can copy the file from my resources to "C:\Test" Folder
AND YES I RUN AS ADMIN BUT STILL ERROR ACCESS IS DENIED IN SECOND ONE ?
im noob im just beginner in vb.net so thanks alot if u cud help me :)
You need to specify the filename in the path. And also, you need to convert the file from resource to Bytes array. Because that's what the WriteAllBytes() function would be expecting
Do a search in Google.
Your Problem Was That You Where Writing To A Null Path Like This "C:\test" , Instead You Have To Include The Extension Of The File You Want To Write
Example If You Have A test.txt File In Your Resources Then You Use It like This
IO.File.WriteAllText("C:\Test.txt", My.Resources.test)

Getting path of exe files from hard drive VB net

Looking for a way that I can get the path of all exe files on an already specified drive. Or, get the path of a specified program.
Eg: specified c drive, program is excel. Code should then find the path for excel.
Or
Eg: gets path of all exe files, puts them in an array, can then search the array for the program that needs to be fetched.
Is there an easy way to do this?
Thanks in advance for any help
Ash
this code returns all exe files full paths from c:\Program Files\Microsoft Office directory into szFiles array then fetch for excel...
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim szFiles() As String, i As UInteger
szFiles = Directory.GetFiles("C:\Program Files\Microsoft Office", "*.exe", SearchOption.AllDirectories)
For i = 0 To szFiles.Length - 1
If InStr(szFiles(i), "EXCEL", CompareMethod.Text) > 0 Then MessageBox.Show("Result found for Excel..." & szFiles(i))
Next
End Sub
End Class
Hope it helps.