繁体   English   中英

.bat文件无法在Windows Server 2003上执行

[英].bat file can not execute on windows server 2003

我想在服务器上执行批处理文件,但问题是它无法执行。 即使它没有给出任何错误。 我该怎么办? 这是我的代码

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);
}

尝试这样-您错过了代码中的一些指令。 请重新检查以下代码。

// 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>")));

您可能需要提升运行应用程序的用户的权限。 Windows 2003阻止执行。 批处理文件是本地文件还是在网络位置?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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