5

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 .

Cœur
  • 37,241
  • 25
  • 195
  • 267
HelloWorld_Always
  • 1,555
  • 3
  • 22
  • 32

4 Answers4

5
string location;

Process p = new Process;

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

p.StartInfo.FileName = location;
HelloWorld_Always
  • 1,555
  • 3
  • 22
  • 32
  • 1
    You could use `Path.Combine` for that concatanation, "optimum" doesen't give you much leeway. – Jodrell Jun 01 '11 at 16:46
3

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Agreed - though since you're starting it from code, you can also fetch the location of your current EXE, regardless of the working folder, by using System.Reflection.Assembly.GetCallingAssembly().Location, and then just append "Resources\batchname.bat" to that. – SqlRyan May 31 '11 at 20:14
  • Yes, @Rwmnau. I should have been more clear. I meant that the program should use a fully qualified path *to the EXE directory* (because that's where the resource folder appears to be), not to the current working directory. – Rob Kennedy May 31 '11 at 20:16
  • Thank you Rob and rwmnau . The System.Reflection.Assembly.GetCallingAssembly().Location method also includes the (.exe file) . is there a way to truncate that. I am thinking getting string size and truncating the size of the exe filename ... any better sugessions – HelloWorld_Always May 31 '11 at 21:38
  • @HelloWorld: http://stackoverflow.com/questions/2412413/how-can-i-get-directory-name-of-a-path – Rob Kennedy May 31 '11 at 21:50
3

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. :)

Community
  • 1
  • 1
ckramer
  • 9,419
  • 1
  • 24
  • 38
1

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.

Doc Brown
  • 19,739
  • 7
  • 52
  • 88