Link to home
Start Free TrialLog in
Avatar of Un-Obtainium
Un-ObtainiumFlag for United States of America

asked on

call a .bat file that is an embedded resource

I have an app that needs to run a .bat file for certain tasks. I would like to make this file an embedded resource in VS2008 so that I know it will be in the correct place when I deploy the app. I dont want the user to have to make sure this file is in the correct directory or even know that this file exists. Is this possible? and how?

Below is the code that works when the file.bat in in the right directory.
Dim oSystemProcess As Diagnostics.Process
 
Dim oPsi As New System.Diagnostics.ProcessStartInfo _
                    ("D:\Test\file.bat", **passed argumenst**) 
 
oSystemProcess = Diagnostics.Process.Start(oPsi)

Open in new window

Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

I would write it out to a text file, start it, and remove the text file after the process has completed.
I have a program that does this with an executable.  

Add the batch file:
    Project menu | Add Existing Item...  (browse for and select your .bat file)
    View Solution Explorer (View menu | Solution Explorer)
    Right-Click your .bat file in Solution Explorer, select Properties
    Set Build Action to "Embeded Resource"


Here's my source, you can change it for a .bat:

    Private Shared Function CreateExecutable() As String

        'Create a temporary path for our executable
        Dim tempPath As String = IO.Path.ChangeExtension(IO.Path.GetTempFileName(), ".exe")

        'Get the Current Assembly information
        Dim currentAssembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()

        'Resource will store the full name of our embeded dpe.exe resource file
        Dim resource As String = String.Empty

        'Get all resource names and find the full embeded dpe.exe resource file name
        Dim arrResources As String() = currentAssembly.GetManifestResourceNames()
        For Each resource In arrResources

            If resource.IndexOf("dpe.exe") > -1 Then _
                Exit For

        Next


        'Open the Embeded dpe.exe resource
        Dim resourceStream As IO.Stream = currentAssembly.GetManifestResourceStream(resource)

        'Open FileStream to write out
        Dim writer As New IO.FileStream(tempPath, IO.FileMode.Create, IO.FileAccess.Write)


        'Write out all bytes of the stream
        Const size As Int16 = 4096
        Dim bytes(size) As Byte
        Dim numBytes As Int32 = 0

        Do
            numBytes = resourceStream.Read(bytes, 0, size)

            writer.Write(bytes, 0, numBytes)

        Loop While (numBytes > 0)


        'Close both streams and clean up
        resourceStream.Close()
        resourceStream.Dispose()

        writer.Close()
        writer.Dispose()

        Return tempPath

    End Function

You can do the rest like this:

Dim oSystemProcess As Diagnostics.Process
Dim batchFile as String = CreateExecutable()

Dim oPsi As New System.Diagnostics.ProcessStartInfo _
                    (batchFile, **passed argumenst**)
 
oSystemProcess = Diagnostics.Process.Start(oPsi)
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial