【问题标题】:.bat file can not execute on windows server 2003.bat 文件无法在 Windows Server 2003 上执行
【发布时间】:2010-12-25 05:05:55
【问题描述】:

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

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

【问题讨论】:

  • 你确定你的代码没有抛出不是Win32Exception的异常吗?
  • 它抛出一个错误访问被拒绝。

标签: asp.net windows-server-2003 executable batch-file


【解决方案1】:

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

【讨论】:

  • 所有可以使用我的应用程序的用户,应用程序应该被执行。我怎样才能给所有用户特权?请告诉我
【解决方案2】:

像这样尝试 - 你错过了代码中的一些指令。请用下面的代码重新检查。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多