Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


call a .bat file that is an embedded resource

Posted on 2009-02-17
Medium Priority
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
  • 2
  • 2
LVL 33

Expert Comment

by:Todd Gerbert
ID: 23663473
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

ID: 23664229
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

ID: 23664244
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

Todd Gerbert earned 2000 total points
ID: 23664481
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


Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

580 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