C# 在ASP.NET中执行命令行实用程序

C# 在ASP.NET中执行命令行实用程序,c#,.net,asp.net,C#,.net,Asp.net,我需要一些关于从C#/ASP.NET web应用程序使用命令行实用程序的建议 我发现了一个第三方实用程序,用于将文件转换为CSV格式。该实用程序工作正常,可以从命令行使用 我一直在网上寻找有关如何执行命令行实用程序的示例,并找到了一个示例 问题是这不是很好。当我尝试将示例代码与我的实用程序结合使用时,会收到一条提示,要求我在客户机上安装该实用程序。这不是我想要的。我不想让用户看到后台发生了什么 是否可以在服务器端执行命令并从那里处理文件 任何帮助都将不胜感激 该命令正在服务器端运行。服务器上正在

我需要一些关于从C#/ASP.NET web应用程序使用命令行实用程序的建议

我发现了一个第三方实用程序,用于将文件转换为CSV格式。该实用程序工作正常,可以从命令行使用

我一直在网上寻找有关如何执行命令行实用程序的示例,并找到了一个示例

问题是这不是很好。当我尝试将示例代码与我的实用程序结合使用时,会收到一条提示,要求我在客户机上安装该实用程序。这不是我想要的。我不想让用户看到后台发生了什么

是否可以在服务器端执行命令并从那里处理文件


任何帮助都将不胜感激

该命令正在服务器端运行。服务器上正在运行任何代码。您给出的示例中的代码有效。您只需确保在服务器上正确设置了该实用程序,并且您拥有对目录/文件的权限。

我以前做过几次类似的事情,下面是对我有效的方法:

创建IHttpHandler实现(作为.ashx文件最容易实现)来处理转换。在处理程序中,使用System.Diagnostics.Process和ProcessStartInfo运行命令行实用程序。您应该能够将标准输出重定向到HTTP响应的输出流。下面是一些代码:

public class ConvertHandler : IHttpHandler
{
    #region IHttpHandler Members

    bool IHttpHandler.IsReusable
    {
        get { return false; }
    }

    void IHttpHandler.ProcessRequest(HttpContext context)
    {
        var jobID = Guid.NewGuid();

        // retrieve the posted csv file
        var csvFile = context.Request.Files["csv"]; 

        // save the file to disk so the CMD line util can access it
        var filePath = Path.Combine("csv", String.Format("{0:n}.csv", jobID));
        csvFile.SaveAs(filePath);

        var psi = new ProcessStartInfo("mycsvutil.exe", String.Format("-file {0}", filePath))
        {
            WorkingDirectory = Environment.CurrentDirectory,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true                
        };

        using (var process = new Process { StartInfo = psi })
        {
            // delegate for writing the process output to the response output
            Action<Object, DataReceivedEventArgs> dataReceived = ((sender, e) =>
            {
                if (e.Data != null) // sometimes a random event is received with null data, not sure why - I prefer to leave it out
                {
                    context.Response.Write(e.Data);
                    context.Response.Write(Environment.NewLine);
                    context.Response.Flush();
                }
            });

            process.OutputDataReceived += new DataReceivedEventHandler(dataReceived);
            process.ErrorDataReceived += new DataReceivedEventHandler(dataReceived);

            // use text/plain so line breaks and any other whitespace formatting is preserved
            context.Response.ContentType = "text/plain";

            // start the process and start reading the standard and error outputs
            process.Start();
            process.BeginErrorReadLine();
            process.BeginOutputReadLine();

            // wait for the process to exit
            process.WaitForExit();

            // an exit code other than 0 generally means an error
            if (process.ExitCode != 0)
            {
                context.Response.StatusCode = 500;
            }
        }
    }

    #endregion
}
public类ConvertHandler:IHttpHandler
{
#区域IHttpHandler成员
布尔·伊赫特芬德勒是可维修的
{
获取{return false;}
}
void IHttpHandler.ProcessRequest(HttpContext上下文)
{
var jobID=Guid.NewGuid();
//检索发布的csv文件
var csvFile=context.Request.Files[“csv”];
//将文件保存到磁盘,以便CMD行util可以访问它
var filePath=Path.Combine(“csv”,String.Format(“{0:n}.csv”,jobID));
csvFile.SaveAs(文件路径);
var psi=new ProcessStartInfo(“mycsvutil.exe”,String.Format(“-file{0}”,filePath))
{
WorkingDirectory=Environment.CurrentDirectory,
UseShellExecute=false,
重定向标准输出=真,
RedirectStandardError=true,
CreateNoWindow=true
};
使用(var进程=新进程{StartInfo=psi})
{
//用于将流程输出写入响应输出的委托
收到的操作数据=((发送方,e)=>
{
if(e.Data!=null)//有时接收到带有null数据的随机事件,不确定原因-我宁愿不使用它
{
context.Response.Write(如数据);
context.Response.Write(Environment.NewLine);
context.Response.Flush();
}
});
process.OutputDataReceived+=新的DataReceivedEventHandler(dataReceived);
process.ErrorDataReceived+=新的DataReceivedEventHandler(dataReceived);
//使用文本/纯文本,以便保留换行符和任何其他空白格式
context.Response.ContentType=“text/plain”;
//开始该过程并开始读取标准和错误输出
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
//等待进程退出
process.WaitForExit();
//0以外的退出代码通常表示错误
如果(process.ExitCode!=0)
{
context.Response.StatusCode=500;
}
}
}
#端区
}

你说的是基于web的应用程序,对吗?在使用第三方实用程序处理文件之前,您是否已将文件上载到服务器上?是的,这是一个web应用程序。在运行该实用程序之前,我确实将其上传到了服务器。还是不高兴谢谢你丹尼尔,我现在正在尝试