Kristal Luna
Kristal Luna

Reputation: 25

How to reference batch files stored in project from code

I have a VB App that is calling .bat files externally, I figured out how to add the bat files to the project, but how do I reference their location in code vs externally referenced outside of the solution folder

Ex

My.code.reference.location(somefile.bat)
not >>> Process.Start("C:\Users\person\file.bat")

my actual work

Try

Dim startInfo As New ProcessStartInfo("cmd")

startInfo.WindowStyle = ProcessWindowStyle.Hidden

startInfo.Arguments = "/C C:\Users\luna\install.bat"

startInfo.RedirectStandardOutput = True

startInfo.UseShellExecute = False

startInfo.CreateNoWindow = True

Dim p = Process.Start(startInfo)

Dim result = p.StandardOutput.ReadToEnd()

p.Close()

MsgBox("Service Installed")

Catch ex As Exception

MsgBox("Error")

End Try

my question is, the line where it points to a path and a batch file, how do i include the batch file in the project and script it where its referenced internally vs the path its pointing to now

Upvotes: 1

Views: 39

Answers (1)

uannabi
uannabi

Reputation: 184

This might help, Its worth trying;

  • Right-click your project in Solution Explorer → Add → Existing Item.
  • Select your .bat file.
  • Set its Build Action to Content and Copy to Output Directory to Copy Always.

Reference the File in the Code:

Use a relative path to the .bat file in your project directory:

Dim batFilePath As String = Path.Combine(Application.StartupPath, "install.bat")
Dim startInfo As New ProcessStartInfo("cmd") With {
    .WindowStyle = ProcessWindowStyle.Hidden,
    .Arguments = "/C " & batFilePath,
    .RedirectStandardOutput = True,
    .UseShellExecute = False,
    .CreateNoWindow = True
}
Dim p = Process.Start(startInfo)
Dim result = p.StandardOutput.ReadToEnd()
p.Close()
MsgBox("Service Installed")

Upvotes: 1

Related Questions