Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

batch-file

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 .

I tried this

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

and this

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

Both don't work .

like image 224
HelloWorld_Always Avatar asked May 31 '11 20:05

HelloWorld_Always


People also ask

Where do I put .bat files?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to run a Windows 10 batch file and press Enter: C:\PATH\TO\FOLDER\BATCH-NAME. bat.

How do I run a Windows batch file in C#?

As SA told, use System. Diagnostics. Process namesapce to runout your batch file.

How do I open a batch file in Visual Studio?

To fix this simply run the batch file, vsvars32. bat that comes with Visual Studio.NET from the command line in the working folder. After you run this batch file devenv.exe will be available from the command line in that folder.


2 Answers

string location;

Process p = new Process;

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

p.StartInfo.FileName = location;
like image 80
HelloWorld_Always Avatar answered Sep 24 '22 00:09

HelloWorld_Always


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.

like image 28
Rob Kennedy Avatar answered Sep 23 '22 00:09

Rob Kennedy