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

前端 未结 4 895
轻奢々
轻奢々 2021-01-06 13:25

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

相关标签:
4条回答
  • 2021-01-06 13:39

    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
    0 讨论(0)
  • 2021-01-06 13:41

    of course :

    you should run it but with command.com before

    System.Diagnostics.Process.Start("cmd", "/c c:\test\batchFile.bat");
    
    0 讨论(0)
  • 2021-01-06 13:49

    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.

    0 讨论(0)
  • 2021-01-06 13:53

    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.

    0 讨论(0)
提交回复
热议问题