简体   繁体   中英

.bat file can not execute on windows server 2003

I want to execute the batch file on server but the problem is that it can not execute. Even it is not giving any error. What should I do? this is my code

try
{
    ProcessStartInfo info = new ProcessStartInfo(AppPath + @"bin\execute.bat");
    info.UseShellExecute = false;
    info.RedirectStandardInput = true;
    info.RedirectStandardError = true;
    info.RedirectStandardOutput = true;
    info.CreateNoWindow = false;

    //info.WindowStyle = ProcessWindowStyle.Minimized;
    info.WorkingDirectory = AppPath + @"bin";
    using (Process install = Process.Start(info))
    {
        string output = install.StandardOutput.ReadToEnd();
        install.WaitForExit();
        Response.Write(output);
        Console.WriteLine(output);
        string strError = install.StandardError.ReadToEnd();
        if (install.ExitCode == 0)
        {
           // Ok = true;
        }
        else
        {
            Response.Write(" Running failed. Description: " + strError);
        }

    }
}
catch (Win32Exception e)
{
     Response.Write("W32 Error:" + e.NativeErrorCode.ToString() + "." + e.Message);
}

Try like this - u have missed few instructions in the code. kindly recheck with below code.

// Get the full file path
string strFilePath = “c:\\temp\\test.bat”;

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);


// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath); 

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;


// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();


// Close the io Streams;
sIn.Close(); 
sOut.Close();


// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));

You will probably need to elevate the privileges of the user running your application. Windows 2003 is blocking the execution. Is the batch file local or on a network location?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM