Results 1 to 10 of 10

Thread: Bat file to run in exe (like shell)

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    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,

  6. #6
    New Member
    Join Date
    Mar 2012
    Posts
    3

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Bat file to run in exe (like shell)

    Quote Originally Posted by jimbrown View Post
    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.

  8. #8
    New Member
    Join Date
    Mar 2012
    Posts
    3

    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.

  9. #9
    Addicted Member
    Join Date
    Jan 2012
    Posts
    190

    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.

  10. #10
    New Member
    Join Date
    Mar 2012
    Posts
    3

    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
  •  



Click Here to Expand Forum to Full Width