c# - Web 应用程序 Asp.Net : Execute a Dos Command

标签 c# asp.net web-applications command

我有一个 Web 应用程序,我想在其中向命令行发送命令(命令未知)。这是我使用的方法

    public static string ExecuteCommand(string command)
    {
        String result;
        try
        {
            //Create a new ProcessStartInfo
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
            //Settings
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = true;
            //Create new Process
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            //Set ProcessStartInfo
            proc.StartInfo = procStartInfo;
            //Start Process
            proc.Start();
            //Wait to exit
            proc.WaitForExit();
            //Get Result
            result = proc.StandardOutput.ReadToEnd();
            //Return
            return result;
        }
        catch
        {
        }
        return null;
    }

该命令适用于控制台应用程序,但不适用于 Web 应用程序(返回 null)

    public string test = "NOTHING";

    protected void Page_Load(object sender, EventArgs e)
    {
        test = AppCommandHandler.ExecuteCommand("mkdir test2");
    }

我做错了什么?我看的每个教程都告诉我使用 ProcessStartInfo

编辑 我不断收到此错误:

{"StandardOut has not been redirected or the process hasn't started yet."}

最佳答案

Web 应用程序的池是否有足够的权限来执行此命令?

关于c# - Web 应用程序 Asp.Net : Execute a Dos Command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7027700/

相关文章:

c# - WPF 自定义数据网格列标题

c# - 在 .netcore 中使用同一个 nuget 包的多个版本

java - 多张图片上传(Play Framework)

c# - rowdatabound 中的 gridview id

c# - 无法获取属性值 'click' : object is null or undefined

php - Ruby on Rails 或 PHP 框架

javascript - 如何在不害怕的情况下更新 JavaScript 库?

c# - 将不同的数字类型作为参数发送到方法时是否存在性能问题?

c# - 如何将按钮放在可以在 Windows 8.1 应用程序中点击的网格中?

c# - 从 ASP.NET 中的代码隐藏变量中为 SQL Server 中的 Select Top 设置变量