ASP.NET 运行 EXE 文件
我正在尝试从 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();
}
-1066598274
我得到的退出代码是: 结果变量为空。 没有抛出异常 我正在使用带有 IIS 7.0 的 Windows 2008
提前致谢,
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.
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
Thanks in advance,
Ezequiel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不要这样做。 这只是普通的肮脏行为,不应该从 ASP.NET 完成。
不要这样做。 这非常糟糕,不可扩展,而且对 Web 服务器不利
不要 不要
不要
不要
Don't do this. This is just plain dirty and should not be done from ASP.NET
Don't do this. This is very bad and not scalable and bad for the web server
Don't
Don't
Don't
如果使用,
则必须在进程执行时读取流,而不是在调用
Same 之前读取标准错误流。 有关更多详细信息,请参阅 MSDN 文档。
If you use
then you have to read the stream as the process executes, instead of before the call to
Same goes for the standard error stream. See the MSDN docs for more detail.
您需要在最后重新排序输出读数。
它希望您在 waitforexit() 调用之前阅读,因此您应该:
You need to reorder the output reading at the end.
It expects you to read before the waitforexit() call, so you should have:
如果您尝试运行的应用程序确实是您所说的 .NET 应用程序,那么您可能根本不需要在单独的进程中运行它。 相反,您可以利用 .NET 可执行文件也是程序集这一事实。 我认为 Visual Studio 不会让您引用以 .exe 结尾的程序集,但命令行编译器会。
我将尝试使用命令行编译器创建一个包装器程序集,该程序集仅引用可执行程序集,并直接调用其 Main() 方法,传入您通常指定的任何命令行参数的字符串数组。 退出代码(如果有)将是 Main 方法的整数返回值。 然后,您可以简单地从 ASP.NET 应用程序调用包装器程序集。
根据可执行文件的作用以及它与控制台的交互程度,这种方法可能根本不起作用。 但如果它确实适合您的情况,那么它的性能应该比启动单独的流程要好得多。
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.
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.
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.
我所做的就是让 ms sql 作业调用可执行文件。
可执行文件将作为 SQL Server 代理服务帐户运行。
新作业可以使用以下命令调用EXEC msdb.dbo.sp_start_job @jobname,其中 @jobname 是
携带您要启动的作业名称的变量。
注意,当这个作业启动时,exe的UI会被隐藏,不会显示; 但您可以在任务管理器中找到它。
我在几个应用程序中使用了这种方法,尤其是无法在网页上完成的耗时操作。
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.
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.
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.
I have employed this method in several applications especially time consuming operations that cannot be done on the web page.
您可能需要将 proc.StartInfo.LoadUserProfile 属性设置为 true ,以便将管理员的用户配置文件内容加载到注册表中(据我所知,默认情况下不会发生这种情况)。
此外,运行“hello world”程序来查看问题是否出在实际创建进程或进程本身在给定上下文中运行时是否存在问题可能是有教育意义的。
最后,作为尝试缩小问题范围的一步,您可能需要使用管理员或系统凭据运行 ASP.NET 进程本身,以查看运行 ASP.NET 实例的帐户的权限中是否存在某些内容是问题的一部分(但请执行此操作仅用于故障排除)。
You may need to set the
proc.StartInfo.LoadUserProfile
property totrue
so the administrator's user profile stuff is loaded into the registry (AFAIK this does not happen by default).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.
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).
使用下面的代码:
Use below code: