Is it possible to embed a bat file in .exe and use

2019-02-15 17:58发布

I've created a batch file that is being used with a Process. I currently just have the application pointing to a directory on my local machine.

Rather than having to bundle my exe with this batch, I would like to just embed it as a resource so that it will be contained in the exe. Is this possible with a batch file?

ProcessStartInfo startInfo = new ProcessStartInfo()

startInfo.FileName = "c:\test\batchFile.bat";

// set up and execute batch file with various arguments

I've added the batch file to my solution, and added it as a "resource" to my project file. I'm not entirely sure this is the way to go, or how to access this in my project.

4条回答
淡お忘
2楼-- · 2019-02-15 18:34

As for embedding the batch file - you can embed just about anything if need be... the answer to this is yes :-)

As for how to use the embedded batch file - the easiest option is the read it from the resource (see http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager.aspx and http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcestream.aspx) and save it as a real file - then use that.

IF you would embed a .NET EXE/DLL you could use that without saving it as a real file... with a batch file I suspect you will need to save it as a real file.

查看更多
太酷不给撩
3楼-- · 2019-02-15 18:45

of course :

you should run it but with command.com before

System.Diagnostics.Process.Start("cmd", "/c c:\test\batchFile.bat");
查看更多
叛逆
4楼-- · 2019-02-15 18:51

How about this approach:

  • store your batch file in your program (as resource or string constant or ...)
  • save the contents to a temporary file
  • run this temporary batch file
查看更多
做自己的国王
5楼-- · 2019-02-15 19:01

Probably the most bullet-proof way to do this is to write the .bat out to a temporary file, then run that temporary file.

cmd.exe is, in my experience, rather persnickety and can behave different ways in different contexts. If you want it to behave just like a .bat file that you would run from your filesystem, then you should run the .bat file from your filesystem.

You could try (but I would not recommend) piping the commands to an instance of cmd.exe, but as I say, you may get different behavior.

查看更多
登录 后发表回答