C# ASP.NET 运行 EXE 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1039297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET running an EXE File
提问by Ezequiel
I′m trying to run an old .NET application from an ASP.NET website. After reading the web and Stackoverflow (for similar problem) I come to the following code. The Problem is that I get always an error code (I am using administrator account just to testing purposes). If I run the exe manually it works ok.
我正在尝试从 ASP.NET 网站运行旧的 .NET 应用程序。在阅读了网络和 Stackoverflow(针对类似问题)后,我来到了以下代码。问题是我总是得到一个错误代码(我使用管理员帐户只是为了测试目的)。如果我手动运行exe,它可以正常工作。
private void Execute(string sPath)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.UserName = "administrador";
string pass = ".............";
System.Security.SecureString secret = new System.Security.SecureString();
foreach (char c in pass) secret.AppendChar(c);
proc.StartInfo.Password = secret;
proc.StartInfo.WorkingDirectory = ConfigurationManager.AppSettings["WORKINGDIRECTORY"].ToString();
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = sPath;
proc.Start();
proc.WaitForExit();
string result = proc.StandardOutput.ReadToEnd();
Response.Write(result + " - " + proc.ExitCode);
proc.Close();
}
}
}
The exitcode I get is: -1066598274 Result variable is empty. No exception is thrown I am using Windows 2008 with IIS 7.0
我得到的退出代码是:-1066598274 结果变量为空。没有例外我正在使用带有 IIS 7.0 的 Windows 2008
Thanks in advance,
提前致谢,
Ezequiel
以泽奎尔
采纳答案by Michael Burr
You may need to set the proc.StartInfo.LoadUserProfile
property to true
so the administrator's user profile stuff is loaded into the registry (AFAIK this does not happen by default).
您可能需要将该proc.StartInfo.LoadUserProfile
属性设置为,true
以便将管理员的用户配置文件加载到注册表中(AFAIK 默认情况下不会发生这种情况)。
Also, it might be educational to run a 'hello world' program to see if the problem is with actaully creating the process or if the process itself is having problems running in the context it's given.
此外,运行“hello world”程序以查看问题是否与实际创建流程有关,或者流程本身在给定的上下文中运行时是否存在问题,这可能是有教育意义的。
Finally, as a step in trying to narrow down where the problem might be, you might want to run the ASP.NET process itself with admin or system credentials to see if something in the permissions of the account the ASP.NET instance is running under is part of the problem (but please do this only for troubleshooting).
最后,作为尝试缩小问题范围的一步,您可能希望使用管理员或系统凭据运行 ASP.NET 进程本身,以查看 ASP.NET 实例所运行帐户的权限中是否存在某些内容是问题的一部分(但请仅在故障排除时执行此操作)。
回答by Shafqat Ahmed
Don't do this. This is just plain dirty and should not be done from ASP.NET
不要这样做。这只是很脏,不应该从 ASP.NET 完成
- Write a windows service
- Store the request in a queue
- The service should poll the queue and process. If needed run the exe. It is suggested that the service stays in a different server.
- 写一个windows服务
- 将请求存储在队列中
- 该服务应该轮询队列和进程。如果需要,运行 exe。建议将该服务保留在不同的服务器中。
Don't do this. This is very bad and not scalable and bad for the web server
不要这样做。这非常糟糕,不可扩展,对 Web 服务器不利
Don't
别
Don't
别
Don't
别
回答by Oliver Mellet
回答by Fiona - myaccessible.website
You need to reorder the output reading at the end.
您需要在最后对输出读数重新排序。
It expects you to read before the waitforexit() call, so you should have:
它希望您在 waitforexit() 调用之前阅读,因此您应该:
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Response.Write(result + " - " + proc.ExitCode);
proc.WaitForExit();
proc.Close();
回答by Joel Mueller
If the application you're trying to run is really a .NET application as you say, you may not need to run it in a separate process at all. Instead, you can take advantage of the fact that .NET executables are also assemblies. I don't think Visual Studio will let you reference assemblies that end in .exe, but the command-line compiler will.
如果您尝试运行的应用程序确实如您所说的 .NET 应用程序,则您可能根本不需要在单独的进程中运行它。相反,您可以利用 .NET 可执行文件也是程序集这一事实。我认为 Visual Studio 不会让您引用以 .exe 结尾的程序集,但命令行编译器会。
I would try using the command-line compiler to create a wrapper assembly that simply references the executable assembly, and directly calls its Main() method, passing in a string array of any command-line parameters you would normally specify. The exit code, if any, will be an integer return value from the Main method. Then you can simply call your wrapper assembly from your ASP.NET app.
我会尝试使用命令行编译器来创建一个简单引用可执行程序集的包装程序集,并直接调用其 Main() 方法,传入您通常指定的任何命令行参数的字符串数组。退出代码(如果有)将是 Main 方法的整数返回值。然后您可以简单地从您的 ASP.NET 应用程序调用您的包装程序集。
Depending on what the executable does, and how much it interacts with the console, this approach may not work at all. But if it does work for your case, it should perform much better than spinning up a separate process.
根据可执行文件的作用以及它与控制台交互的程度,这种方法可能根本不起作用。但是,如果它确实适用于您的情况,那么它的性能应该比启动单独的过程要好得多。
回答by logical
Use below code:
使用以下代码:
ProcessStartInfo info = new ProcessStartInfo("D:\My\notepad.exe");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
//info.UserName = dialog.User;
info.UserName = "xyz";
string pass = "xyz";
System.Security.SecureString secret = new System.Security.SecureString();
foreach (char c in pass)
secret.AppendChar(c);
info.Password = secret;
using (Process install = Process.Start(info))
{
string output = install.StandardOutput.ReadToEnd();
install.WaitForExit();
// Do something with you output data
Console.WriteLine(output);
}
回答by David Jiboye
What i do is to have the executable called by a ms sql job. The executable would be run as SQL server agent service account.
我所做的是让 ms sql 作业调用可执行文件。可执行文件将作为 SQL 服务器代理服务帐户运行。
- Create a new sql server job
- Give it a name in the job property's general page
- In the steps page, create a new step of type Operating system (CmdExec)
- Speeify the command and click ok to save the job parameters
- 创建一个新的 sql server 作业
- 在作业属性的常规页面中为其命名
- 在步骤页面中,创建一个类型为操作系统 (CmdExec) 的新步骤
- 指定命令,点击ok保存作业参数
The new job can be called using EXEC msdb.dbo.sp_start_job @jobname, where @jobname is the variable carrying the name of the job you want to start.
可以使用 EXEC msdb.dbo.sp_start_job @jobname 调用新作业,其中 @jobname 是带有要启动的作业名称的变量。
Note that when this job starts, the UI of the exe will be hidden and will not be displayed; but you can find it in your task manager.
注意这个作业启动时,exe的UI会被隐藏,不会显示;但是你可以在你的任务管理器中找到它。
I have employed this method in several applications especially time consuming operations that cannot be done on the web page.
我在几个应用程序中使用了这种方法,特别是在网页上无法完成的耗时操作。
回答by Dariush Tasdighi
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
litMessage.Visible = true;
System.Diagnostics.Process oProcess = null;
try
{
string strRootRelativePathName = "~/Application.exe";
string strPathName =
Server.MapPath(strRootRelativePathName);
if (System.IO.File.Exists(strPathName) == false)
{
litMessage.Text = "Error: File Not Found!";
}
else
{
oProcess =
new System.Diagnostics.Process();
oProcess.StartInfo.Arguments = "args";
oProcess.StartInfo.FileName = strPathName;
oProcess.Start();
oProcess.WaitForExit();
System.Threading.Thread.Sleep(20000);
litMessage.Text = "Application Executed Successfully...";
}
}
catch (System.Exception ex)
{
litMessage.Text =
string.Format("Error: {0}", ex.Message);
}
finally
{
if (oProcess != null)
{
oProcess.Close();
oProcess.Dispose();
oProcess = null;
}
}
}
}