Is it possible to embed a bat file in .exe and use it with the Process class?

只谈情不闲聊 提交于 2019-11-30 19:49:14

问题


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.


回答1:


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.




回答2:


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



回答3:


of course :

you should run it but with command.com before

System.Diagnostics.Process.Start("cmd", "/c c:\test\batchFile.bat");



回答4:


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.



来源:https://stackoverflow.com/questions/7798447/is-it-possible-to-embed-a-bat-file-in-exe-and-use-it-with-the-process-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!