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