开发者

Can I put a windows (.bat ) file inside the resources folder of a Visual studio C# project?

Although one can run a batch file form c: location , i would like to know if its possible to have the .bat file inside the resources folder 开发者_如何学Go.

I tried this

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

and this

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

Both don't work .


string location;

Process p = new Process;

location = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
+@"\Resources\batchfile.bat";

p.StartInfo.FileName = location;


You can put a batch file in whatever file-system location you want.

It looks like the path to your batch file is wrong, though. Paths with a leading backslash are interpreted relative to the root directory of the current drive. That's probably not where your batch file is, though. It's probably in the Resources subdirectory of your application's own installation folder. At the very least, remove the leading backslashes from those strings. Then they will be interpreted relative to your process's current working directory.

It would be better to use a fully qualified path, though. The current working directory has a tendency to change when you're not expecting.


You would want to include the .bat file as an embedded resource. So in Visual Studio you would open up the Properties on the file, and select "Embedded Resource" for the "Build Action".

Now for the fun part....in your app you'll want to extract the file and write it to disk prior to executing using the GetManifestResourceStream method on the Assembly object. This is slightly tricky because you need to pass a resource name to the method, and that name ends up being based on your assemblies namespace, plus the path (so if your project is MyProject and your file in in Resources\MyBat.bat then the resource name would be MyProject.Resources.MyBat.bat...at least I think thats right)

There is actually an existing SO question about how to do this here, and has a much nicer code sample than the one I was going to whip up. :)


Resources (of your C# project) are typically files linked into your assembly at compile time. If you put a batch file there, you have to extract it at run-time to a folder like the %TEMP% folder first and run it from there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜