0
public void runBatchfile(String batchfilename)
{
   try
   {
      ProcessStartInfo processInfo = new ProcessStartInfo(batchfilename);
      processInfo.UseShellExecute = false;
      Process batchProcess = new Process();
      batchProcess.StartInfo = processInfo;
      batchProcess.StartInfo.CreateNoWindow = true;
      batchProcess.Start();
      batchProcess.WaitForExit();
   }
   catch (Exception r) { }
}

runBatchfile(@"c:\lol.bat");

lol.bat contains these 2 lines

dir c:\ /s /b > c:\filelist.txt
exit

and when I run my code all it does is creating a filelist.txt file, but doesn't actually perform the rest of the command, which does work if I manually insert it into CMD.

Btw I've tried making the extension .cmd and I also tried without the exit command, without any other results.

please help me :)

1
  • 2
    Have you tried executing CMD.EXE instead, passing /C lol.bat to it as arguments? Commented Mar 3, 2011 at 21:38

2 Answers 2

1

On my Windows 7 64-bit machine with Visual Studio 2010, running this code straight from the IDE doesn't do anything whatsoever.

On a hunch that it might have something to do with permission to write to the root directory of drive C:, I tried running the .exe directly from an Explorer window with admin rights (right-click, Run as Administrator) and that worked.

I'd say it's a permission problem.

Maybe you could redirect the output to a file located elsewhere?

update:

I changed the batch file so that the dir command gets redirected to my desktop and it runs just fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, you can't do snot on the C:\ root.
0

I've had issues with this as well when calling from C#. The workaround that I usually use is to physically allow it to show the window when running the command. Not sure why this makes a difference but it has worked for me in the past.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.