Un-Obtainium
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.
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)
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.GetT empFileNam e(), ".exe")
'Get the Current Assembly information
Dim currentAssembly As Reflection.Assembly = Reflection.Assembly.GetExe cutingAsse mbly()
'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.GetManifes tResourceN ames()
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.GetManifes tResourceS tream(reso urce)
'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
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
'Get the Current Assembly information
Dim currentAssembly As Reflection.Assembly = Reflection.Assembly.GetExe
'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.GetManifes
For Each resource In arrResources
If resource.IndexOf("dpe.exe"
Exit For
Next
'Open the Embeded dpe.exe resource
Dim resourceStream As IO.Stream = currentAssembly.GetManifes
'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,
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.Process StartInfo _
(batchFile, **passed argumenst**)
oSystemProcess = Diagnostics.Process.Start( oPsi)
Dim oSystemProcess As Diagnostics.Process
Dim batchFile as String = CreateExecutable()
Dim oPsi As New System.Diagnostics.Process
(batchFile, **passed argumenst**)
oSystemProcess = Diagnostics.Process.Start(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.