Getting path of exe files from hard drive VB net - 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.

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.

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

VB Folder: Access Denied (with admin rights)

i'm using VB.NET language on windows 10 with VS 2015
I'm trying to make a directory then copy a file from my app's resources folder to that directory
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SubFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Main Folder\Sub Folder")
Directory.CreateDirectory(SubFolderPath)
'Error: access denied to "C:\Program Files\Main Folder\Sub Folder"
File.WriteAllBytes(SubFolderPath, My.Resources.exe1)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
File.WriteAllBytes(SubFolderPath, My.Resources.exe2)
File.WriteAllBytes(SubFolderPath, My.Resources.exe2dat)
End Sub
i get error as commented in the above code, (i have admin rights)
Code result: created folder "C:\Program Files\Main Folder\Sub Folder" but then access denied while copying.
i'm not knowing why access is denied... can you help me please?
The problem with your code is that you specify a directory name instead of a file name as the first argument of the File.WriteAllBytes methods:
File.WriteAllBytes(SubFolderPath, My.Resources.exe1)`
Do something like this to correct it:
File.WriteAllBytes(SubFolderPath & "\exe1.exe", My.Resources.exe1)
File.WriteAllBytes(SubFolderPath & "\exe2.exe", My.Resources.exe2)
File.WriteAllBytes(SubFolderPath & "\exe2dat.dat", My.Resources.exe2dat)
And it's not a problem with Byte(). Whenever you import a binary exe to your resources, it is stored as a Byte(). You need not worry about that.

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)

How to check file location in window mobile 6 professional vb.net

i face check txt file location problem in window mobile 6 professional.
code testing for window form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "c:\Users\Chee Kang\Desktop\New folder (2)\testFile.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
MessageBox.Show("File already Exist")
Else
MsgBox("File Does Not Exist")
End If
End Sub
coding above show me the correct answer :File already Exist (i pasted the file there before i start my program)
but when i apply the same thing in window mobile 6 professional, it give me the wrong answer "File Does Not Exist" although i pasted the file there before i start my program.
i try to figure out the reason, but so far i can't get any correct reason.
Kindly advise.
thanks
Windows Mobile does not use drive letters.
Your path does not exist.
When we work with files on WinMo 6, we store and retrieve all files in directories relative to the installed application so that we don't have to worry about the WinMo directory structure.
To get the path to the installed app, you can use code similar to the following:
''' <summary>
''' Because the Windows Mobile doesn't have a current directory, this
''' method provides a way to get to the directory for this assembly.
''' </summary>
Public ReadOnly Property GetBaseDirectory() As String
Get
Dim sName As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
sName = IO.Path.GetDirectoryName(sName)
'
' Support for unit testing on the desktop. CodeBase returns a path with "file:\" at the start
' under Windows, but not under Windows CE. The code that uses this property expects
' the path without the file:\ at the start.
'
If Environment.OSVersion.Platform <> PlatformID.WinCE Then
If sName.StartsWith("file:\") Then
sName = sName.Remove(0, 6)
End If
End If
Return sName
End Get
End Property
Note that this code also supports unit testing on non-CE platforms. Once you get the base directory, you can use standard System.IO functionality to create directories, save files, and access files.
Other than the above code, much of our file interaction logic is the exact same code for WinMo and WinForms.