Forum Discussion

DucTran's avatar
DucTran
Ideator I
6 years ago
Solved

Run a bat file with argument via server method

Hello Everyone, I would like to run a bat file with argument on server via server method. I saw the same topic on forum in several year ago but they also have no solution. https://community.aras....
  • Gopikrishnan's avatar
    6 years ago

    Hi DucTran

    You can use below server method to run the bat file 

    string inputPath = string.Format(@"C:\Backup\Test");   //Directory of the Bat file
    System.Diagnostics.Process command = new System.Diagnostics.Process();
    command.StartInfo.FileName = Path.Combine(inputPath, "Hello.bat"); // Bat file Name
    string outputPath = Path.Combine(inputPath, "output.txt"); //Output file
    command.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\"", inputPath, outputPath, inputPath);
    command.StartInfo.UseShellExecute = false;
    command.StartInfo.CreateNoWindow = true;
    command.StartInfo.WorkingDirectory = inputPath;
    command.Start();
    if (!command.WaitForExit(3000)) //Execution allowed time to prevent system hung
    {
    throw new Exception(string.Format("The process '{0}' is not responding", command.StartInfo.FileName));
    }

    if (command.ExitCode != 0)
    {
    throw new Exception(string.Format("The process '{0}' has finished with error (ErrorCode={1})", command.StartInfo.FileName, command.ExitCode));
    }
    return this;

    Note: If you get access denied error, provide access to that folder to everyone an try

    Thank You

    Gopikrishnan R