call a .bat file that is an embedded resource

Posted on 2009-02-17
Last Modified: 2013-11-26
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

Question by:Un-Obtainium
    LVL 33

    Expert Comment

    by:Todd Gerbert
    I would write it out to a text file, start it, and remove the text file after the process has completed.
    LVL 27

    Expert Comment

    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


            '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

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

                writer.Write(bytes, 0, numBytes)

            Loop While (numBytes > 0)

            'Close both streams and clean up


            Return tempPath

        End Function

    LVL 27

    Expert Comment

    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)
    LVL 33

    Accepted Solution

    I was thinking right-click on your project, go to properties, then resources and add a string resource, named BatchFile for example, then get it in code using:

    Imports System.Diagnostics
    Imports System.IO
    Dim fileName As String = Path.GetTempFileName() & ".bat"
    File.WriteAllText(fileName, Properties.Resources.BatchFile)
    Dim batchProcess As Process = Process.Start(fileName)

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now