Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone,

I am trying to run a batchfile that contains commands to hit a nunit console runner executable. I am using the system.diagnostics.process method to run the batch file

Here is the code:

Path used in method:
C:\Users\Victor\workstride-automated-tests\DextapAutomation\SeleniumDemo\bin\Debug\../../Batchfiles/

Batchfile Name: RandomBatchfile.bat


ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = Directory specified above
startInfo.CreateNoWindow = false;
startInfo.FileName = RandomBatchfile.bat
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "C-Sharp Console application";

try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch (Exception e)
{
Console.WriteLine(
@"Something went wrong when trying to run the tests,
here is the exception: {0} ", e.ToString());
}

What I have tried:

When I run the program it says it cant find the specified file and the directory it references is the native directory for the solution instead of the directory i specify above. I checked to see if the directory is being set before the process begins and it is, so I am stumped. Please Help !
Posted
Updated 22-Aug-16 7:38am
Comments
Philippe Mori 22-Aug-16 18:52pm    
Is it that hard to use a code block to properly format your code...

1 solution

Why are you using two relative directory codes in an absolute folder specification?
Try:
C#
Process.Start(@"C:\Users\Victor\workstride-automated-tests\DextapAutomation\SeleniumDemo\Batchfiles\RandomBatchFile.bat", "C-Sharp Console application");
And see if that works.
 
Share this answer
 
Comments
VicQA 22-Aug-16 11:45am    
The reason I am using two relative directories is because in order for the batchfile commands to work, the working directory has to be set to the location of the test-solution dll, otherwise it wont run. Even with your suggested fix it still is breaking:

Something went wrong when trying to run the tests,
here is the exception: System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName, String arguments)
at TestRunner.Methods.Util.ExecuteProcess(String client, String directory, String Filename) in C:\Users\Victor\workstride-automated-tests\DextapAutomation\SeleniumDemo\ConsoleRunner\TestRunner\TestRunner\Util.cs:line 115
OriginalGriff 22-Aug-16 11:51am    
That's not a reason for using the "../../" bit in your path - that just gets that far and says "parent of the parent of" the directory you'd got to. Having that in there makes it look like you aren't sure how directories work! :laugh:
So check the actual folder that string refers to and make sure it exists and holds the .BAT file you expect to run. Use the debugger to examine the total string and paste it into Explorer to open the folder.
VicQA 22-Aug-16 13:38pm    
I figured it out on my own, thank you for your input.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900