0

i need to run a batch file (or a cmd) from c# . my code is this simple:

 Process.Start(@"C:\b.bat");
Process.Start(@"cmd.exe");

i have also tried Executing Batch File in C# , and many other sites but i do not think the problem is syntax related.

the error happens for either line above, which I have googled but not found a solution for my particular problem:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll Additional information: The specified executable is not a valid application for this OS platform.

the weird thing is that these codes will run no problem:

Process.Start(@"IExplore.exe");
Process.Start("notepad");

i am running visual studio 2012 and compiling for x86, but my OS is windows 7 64-bit ,not sure if it matters.everything else in the program runs fine.

any help would be greatly appreciated!

Community
  • 1
  • 1
1ak31sha
  • 501
  • 7
  • 18

3 Answers3

1

I think your problem is that you are trying to start an unknown process in Process.Start(@"C:\b.bat"). When I do this i use the following.

        string cmd = "/C b.bat";
        var m_command = new System.Diagnostics.Process();
        m_command.StartInfo.FileName = @"cmd.exe";
        m_command.StartInfo.Arguments = cmd;
        m_command.Start();

The /C will close the applciation after it is finished. So in essence the above code will:

  1. Start the cmd.exe process
  2. Execute your arguments (in this case /C b.bat)
  3. You can also add m_command.WaitForExit() if you want to have the program wait for b.bat to finish executing before continuing.
Evan L
  • 3,805
  • 1
  • 22
  • 31
  • Thanks for replying!! i ran your code, it did not work though. also note that i dont require running a cmd and a bat, from what ive read i should be able to simple execute the bat – 1ak31sha Jun 07 '13 at 21:16
  • No problem, did it work? If so please mark this as the answer by clicking the checkmark below the upvote/downvote buttons. – Evan L Jun 07 '13 at 21:16
  • Ah well in that case, it looks as though the batch file you are trying to execute is not valid. Perhaps re-post the lines in the Batch file? What happens if you run the batch file directly from the command line? Does it execute? Does it give you the same error? – Evan L Jun 07 '13 at 21:21
  • the WaitForExit() will definitely be useful... but ya its the same error still. – 1ak31sha Jun 07 '13 at 21:21
  • the batch file should be fine, i can click it and it does what i want. i cant even open up a blank cmd though...i think if we fix that problem, the batch would run fine as well – 1ak31sha Jun 07 '13 at 21:22
  • Okay, then I'm fairly sure its because you are executing it in 32-bit mode on a 64-bit machine. Not my area of expertise but perhaps try to narrow this down by compiling 64-bit. If that works then it has to do with the platform. Also ensure that your platform settings are correct in configuration manager. I.E. set to x86 rather than Any PC. – Evan L Jun 07 '13 at 21:29
  • Check out [This Post](http://stackoverflow.com/questions/3814713/c-sharp-system-diagnostics-process-cant-launch-a-32-bit-exe-file-in-64-bit-os) Sort of related. – Evan L Jun 07 '13 at 21:32
  • i must compile x86 though, because this app is for other computers which are x86, and also i run SQL queries and the drivers depend on x86. also heres something that is driving me crazy- im pretty sure i ran the command this morning and it worked fine...there must be a way to open the cmd... – 1ak31sha Jun 07 '13 at 21:35
  • ok, so i compiled in x64 and cmd opened. i need x86 though. im not sure what to do atm. i am looking into a way to run a x86 cmd... – 1ak31sha Jun 07 '13 at 21:40
  • If you ran the program from a 32-bit machine (release build) it would work with the x86 build. So, if no users will be running it from 64-bit machines, then don't worry about it and build in x86. – Evan L Jun 07 '13 at 21:43
  • i tried setting the batch file properties to compatibility mode for windows 98 but it didnt work. there will be some 64-bit users though (including myself - it will be hard to make my app without being able to test it...) – 1ak31sha Jun 07 '13 at 21:47
  • im going to use perl script to run the batch - it seems to be getting around this issue. thanks so much for helping me though! you isolated the problem :) – 1ak31sha Jun 07 '13 at 22:03
  • 1
    @user2464951: could you try to manually launch C:\Windows\SysWOW64\cmd.exe? That's the 32 bit cmd.exe, the one that a 32 bit application on x64 will run. It sounds like that file might be corrupt on your system. Alternatively, it could be that another file named cmd.exe is found before the real one in the PATH for 32 bit applications and that one is corrupt. – Sven Jun 07 '13 at 23:27
0

Process.Start(@"C:\b.bat"); ought to work, but putting it into the root may be causing the problem. Try moving the bat file to your project directory, and put the absolute path to the file. For example, I just had success with Process.Start(@"""C:\dev\ConsoleApplication1\ConsoleApplication1\test.bat""");

sfuqua
  • 5,797
  • 1
  • 32
  • 33
0

I solved the problem by running the 32 bit cmd with this command:

proc.StartInfo.FileName = @"C:\windows\system32\cmd.exe";

1ak31sha
  • 501
  • 7
  • 18