Can I put a Windows file (.bat) in the resources folder of a Visual stuido C # project?

Although you can run the c: location batch file format, I would like to know if it is possible to have a .bat file in the resources folder.

I tried this

Process p = new Process; p.StartInfo.FileName = @"\Resources\batchfile.bat"; 

and this one

 p.StartInfo.FileName = @"\Resources\batchfile"; 

Both do not work.

+4
source share
4 answers
 string location; Process p = new Process; location = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) +@ "\Resources\batchfile.bat"; p.StartInfo.FileName = location; 
+2
source

You can put the batch file anywhere on your file system.

It looks like the path to your batch file is incorrect. Paths with leading backslashes are interpreted relative to the root directory of the current drive. This is probably not where your batch file is. There is probably a folder for installation in the Resources subdirectory of your application. At least remove the leading backslashes from these strings. They will then be interpreted relative to the current working directory of the process.

It would be better to use a full path. The current working directory tends to change when you are not expecting.

+3
source

You would like to include the .bat file as an embedded resource. Therefore, in Visual Studio, you open the properties in a file and select "Embedded Resource" for "Build Actions."

Now for the interesting part .... in your application, you will want to extract the file and write it to disk before execution using the GetManifestResourceStream method of the GetManifestResourceStream object. This is a bit complicated because you need to pass the resource name to the method, and that name will be based on the assembly namespace, plus the path (so if your MyProject project and your file are in Resources\MyBat.bat , then the resource name will be MyProject.Resources.MyBat.bat ... at least I think that's right)

Actually there is an existing SO question on how to do this here , and has a much nicer code sample than the one I was about to hack. :)

+3
source

Resources (of your C # project) are usually files associated with your build at compile time. If you put a batch file there, you must extract it at runtime to a folder, for example, to the% TEMP% folder and run it from there.

+1
source

All Articles