-
Apr 17th, 2011, 09:29 PM
#1
Thread Starter
Member
Bat file to run in exe (like shell)
i have this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Process.Start("mybatchfile.bat")
End Sub
its working if they are on the same folder,
i want to do is make the batch file as a resource file so that i can run it even not in the batchs folder?
please edit the code above for the resource file
I am vb6 newbie user and i just shift to vb.net 2010
thanks in advnce
-
Apr 17th, 2011, 09:57 PM
#2
Re: Bat file to run in exe (like shell)
Because you are only specifying a file name and not a path, it assumes that the file is in the applications current working directory. If you want to specify some other folder then that's exactly what you should do: specify the folder. If you want to embed the batch file as a resource then you can do that on the Resources page of the project properties. You will still have to extract the resource contents to a file and run it using Process.Start just as you are now.
-
Apr 17th, 2011, 10:17 PM
#3
Thread Starter
Member
Re: Bat file to run in exe (like shell)
Hi,
thanks, i understand it,
i did specify path before ( Process.Start( "c:\mybatchfile.bat" )and i make it work but now i read the making it as a resource will make it easier for me to run it on the other computer since the batch file is compiled also in the exe,
now i put the batch as a resource but my problem is im having a problem how to run it,
can you please make an example of the code?
tia
Last edited by eyestrain; Apr 17th, 2011 at 10:27 PM.
-
Apr 17th, 2011, 10:42 PM
#4
Re: Bat file to run in exe (like shell)
It won't make it any easier. To execute a batch file you need a batch file. If you embed it as a resource then it will ensure that the data is always available but, as I said, you will need to extract the resource and save it as a file before you can execute it. You can get the resource from the appropriate property of My.Resources. It should be returned as a String. You can then call IO.File.WriteAllText to save that String to a file. You then execute that file as before.
-
Apr 17th, 2011, 10:46 PM
#5
Thread Starter
Member
Re: Bat file to run in exe (like shell)
okey thanks,
let me compile this and I will post result of my code here
thanks again,
-
Mar 19th, 2012, 02:24 PM
#6
New Member
Re: Bat file to run in exe (like shell)
Did you get this to work? I have the same question. here is my sample code:
I want to use embedded resource "test.bat" but understand I need to save resource then run but don't know how to do that.
Public Class Form1
Private Results As String
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)
Private Sub UpdateText()
txtResults.Text = Results
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub
Private Sub CMDAutomate()
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "test.bat" 'starts cmd window
StartInfo.Arguments = (txtCommand.Text)
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True 'creates no cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine(txtCommand.Text) 'the command you wish to run.....
SW.WriteLine("exit") 'exits command prompt window
Results = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
'invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
Private Sub txtResults_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtResults.TextChanged
End Sub
-
Mar 19th, 2012, 07:02 PM
#7
Re: Bat file to run in exe (like shell)
Originally Posted by jimbrown
I need to save resource then run but don't know how to do that.
As posted, you need to call IO.File.WriteAllText and pass the appropriate property of My.Resources.
-
Mar 20th, 2012, 12:00 PM
#8
New Member
Re: Bat file to run in exe (like shell)
Sorry, I am teaching myself vb.net and was hoping for an example open source or suggestion using my code snippet.
-
Mar 20th, 2012, 12:04 PM
#9
Addicted Member
Re: Bat file to run in exe (like shell)
Maybe this
Code:
CMD.StartInfo.FileName = "cmd"
CMD.StartInfo.Arguments = "/C c:\ asdasdadsd"
CMD.StartInfo.UseShellExecute = True
CMD.StartInfo.RedirectStandardInput = True
CMD.StartInfo.RedirectStandardOutput = True
CMD.StartInfo.CreateNoWindow = True
CMD.Start()
Last edited by Vasco Costa; Mar 20th, 2012 at 12:09 PM.
-
Mar 20th, 2012, 01:46 PM
#10
New Member
Re: Bat file to run in exe (like shell)
Thank you Vasco Costa and jmcilhinney for the reply. The app works the way it is now with the above code snippet. my question is how to change it to have a dos batch file as an embedded resource rather then test.bat out side the executable. (I don't want users viewing or changing the batch file) I know dos very well, but am learning vb.net, this vb.net app just runs a batch file gui. I did not write it, it's open source and need more step by step instructions. thanks for any help you can provide.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|