I have an application that i want to run a batch file from it all i get is the flashing CMD windows but is not running.

at one time I get it to run but dont know what happen start modyfying other things and stop working.

what i really want is run the batch file but hidden no user interaction whatsoever and it would be great to have a feedback when the batch finish \

this is one of the million codes I try

System.Diagnostics.ProcessStartInfo p = new
            System.Diagnostics.ProcessStartInfo(@"setstation.cmd");
            p.UseShellExecute = false;
            p.RedirectStandardOutput = false;
            p.RedirectStandardError = true;

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = p;
            proc.Start();
            
            proc.WaitForExit();

my batch file read from a text file and create two files it has a pause on it, it runs great if i run the batch file directly.

Recommended Answers

All 11 Replies

Make sure you put the full path to the batch file. You may also need to call it as

command /c setstation.cmd

how like System.Diagnostics.ProcessStartInfo(@"command c c:\setstation.cmd"); or System.Diagnostics.ProcessStartInfo(@"command c" + "c:\setstation.cmd"); somewhere i view something like \c or /c and then the path to the file or i need to use the word command or i can put this in startInfo.Arguments = "command c"; thanks for your help in advanced

it depends on the batch file i think, but just try "C:\setstation.cmd", you just need the full path.

If c:\setstation.cmd doesnt work, then you may need c:\windows\system32\command.com /c c:\setstation.cmd as your full command, rather than pass it as 1 command you would probably need to call c:\windows\system32\command.com with the parameters of /c c:\setstation.cmd

You should be getting some error codes saying why it failed.

!!!!!! system cannot find file specified.

i get that error on the dos screen i dont get it the file is that path. so
let see i put in StartInfo.FileName="the full path to the batch file and in StartInfo.Arguments = @"c:\windows\system32\command.com /c "; what am missing?

possibly because you have the arguements and the filename round the wrong way? c:\windows\system32\command.com would be the filename, the batchfile is the argument.. as is the /c

Your code also doesnt have the @ so if you wrote c:\test\myfile.cmd it wouldnt have found it either..

Please someone help, this is what i found out so far i was testing with various scenarios and it work simple with this

ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = @"setstation.cmd";
         //  startInfo.Arguments = @"cmd.exe";
           Process.Start(startInfo);

but all this work if the file is in the visual studio application folder next to the application.exe

i think is something with the batch file there are two of them one, the first have a 'call' to the other batch file and a 'type' to a text file. i think this is where the application get lost and throw a system cannot find file specified error. even when i am using the full path in the application code.

its probably upset because you put cmd.exe as your arguement, it might be trying to do something with it.

It will be running the batch file as if it runs from your current directory, this could also give you your file not found if its looking for something in the directory you ran it from.

still not working i end doing this

ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "setstation.cmd";
            
            Process.Start(startInfo);

it works but the batch file need to be in the same directory where the application reside so i change the batch file so it create the files in the folder i want to where it suppose to even when i dont want to do that. its strange that the code doesnt want to execute the batch file from where i put it with the full path to it. just dont get it..

When you showed it before with a path in, your code hadnt told the stirng to be literal so c:\setstation.cmd would have turned to c:setstation.cmd, just as c:\test\setstation.cmd would turn to c:testsetstation.cmd

i try both literal with @ and without it.

anyway i change how the batch behave so i end up needing to call the batch file that reside in the same folder and in the batch file i move, copy and create the files and folder that the app. suppose to do.


thanks a lot !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.