We help IT Professionals succeed at work.

call a .bat file that is an embedded resource

Medium Priority
3,034 Views
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

Comment
Watch Question

Todd GerbertSenior Engineer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I would write it out to a text file, start it, and remove the text file after the process has completed.
Top Expert 2007

Commented:
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

Top Expert 2007

Commented:
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)
Senior Engineer
CERTIFIED EXPERT
Top Expert 2010
Commented:
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)
 
batchProcess.WaitForExit()
 
File.Delete(fileName)

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.