繁体   English   中英

ASP.net如何运行EXE文件?

[英]how to Run EXE file By ASP.net?

我在C#中为ASP.NET创建了一个页面来运行一个exe文件。 我使用以下代码创建了一个按钮和一个事件:

        ProcessStartInfo info = new ProcessStartInfo(@"C:\inetpub\wwwroot\Share\myfile.exe");
        info.UseShellExecute = false;
        info.RedirectStandardInput = true;
        info.RedirectStandardError = true;
        info.RedirectStandardOutput = true;

        //info.UserName = dialog.User;
        info.UserName = "username";
        string pass = "MY pass";
        System.Security.SecureString secret = new System.Security.SecureString();
        foreach (char c in pass)
            secret.AppendChar(c);
        info.Password = secret;
        Process.Start(info);   

我从编译器执行并且它运行正常,但是当我在本地主机上发布我的项目时我没有任何动作。 问题是什么?

你尝试了什么? 这可能是权利问题。 默认情况下,您的网站不应该能够执行外部可执行文件,因为这将是一个严重的安全风险。 也许你的问题有一个不同的解决方案,不涉及运行外部程序?

无论哪种方式,我强烈建议不要直接从您的网站运行可执行文件。 如果你真的必须运行该程序,也许你可以编写一个简单的Windows服务,它可以接收一条消息(通过WCF?),然后在一个隔离的环境中执行你的程序。

要在asp.net中执行(运行)应用程序,我使用了这个方法:

1)查看其他网站上的代码:

要么

2)在web.config中 ,在“ assemblyIdentity ”字段中,我添加了“impersonate”参数,如下所示:

改变了

<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>

<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35" impersonate="true" userName="myuser" password="123123"/>

(首先,您可能需要将sql / windows用户和密码权限设置为该文件夹)。

2)然后我在我的asp.net中使用了命令(也许使用asp 2.0):

System.Diagnostics.Process.Start("C:\\inetpub\\my_application.exe" , "with-any-parameters");

下面是我的示例代码,获取用户输入并执行可执行文件。

<!-- directives -->
<% @Page Language="C#" %>
<!-- code section -->
<script runat="server">

        protected void Test1(object sender, EventArgs e)
        {
        string strValue = Page.Request.Form["mytext"];

        Response.Write(strValue);
        System.Diagnostics.Process.Start(strValue);
        }

</script>
<!-- Layout -->
<html>
   <head> 
      <title> Change to Upper Case </title> 
   </head>
   <body>
      <h3> Conversion to Upper Case </h3>
      <form runat="server">
         <input runat="server" id="mytext" type="text" />
         <input runat="server" id="button1" type="submit" value="Enter..." OnServerClick="Test1"/>
         <hr />
         <h3> Results: </h3>
         <span runat="server" id="changed_text" />
      </form>
   </body>
</html>

======================另一个例子==========================

<!-- directives -->
<%@ Page Language="C#"  validateRequest="false" AspCompat="true" Debug="true" trace="false"%>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Diagnostics" %>
<%@ import Namespace="System.Threading" %>
<%@ import Namespace="System.Net.Sockets" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="Microsoft.Win32" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ Assembly Name="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" %>
<%@ import Namespace="System.DirectoryServices" %>
<%@ import Namespace="System.Security.Cryptography" %>
<!-- code section -->
<script runat="server">

    public string ocmd;
    protected void cmdbtn_Click(object sender, EventArgs e)
    {
        Process pr = new Process();
        pr.StartInfo.FileName = cmdurl.Text;
        pr.StartInfo.RedirectStandardOutput = true;
        pr.StartInfo.UseShellExecute = false;
        pr.StartInfo.Arguments = "/c " + cmd.Text.Trim ();
        pr.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        pr.Start();
    }   

</script>
<!-- Layout -->
<html>
   <head> 
      <title> Change to Upper Case </title> 
   </head>
   <body>
    <form id="form1" runat="server">
      <h3> Conversion to Upper Case </h3>
       <table style="width: 631px; font-size: 12px;">
            <tr>
                <td colspan="3" style="height: 32px">
                    执行CmdShell</td>
            </tr>
            <tr>
                <td style="width: 114px; height: 29px;">
                    CMD:</td>
                <td colspan="2" style="height: 29px" align="left">
                    <asp:TextBox ID="cmdurl" runat="server" Width="320px" Font-Size="12px">cmd.exe</asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 114px; height: 29px;">
                    命令:</td>
                <td colspan="2" align="left">
                    <asp:TextBox ID="cmd" runat="server" Width="320px" Font-Size="12px">Set</asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 114px; height: 158px">
                    回显:</td>
                <td colspan="2" style="height: 158px" align="left">
                    <asp:TextBox ID="cmdshow" runat="server" TextMode="MultiLine" Width="472px" Height="140px" Font-Size="12px"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 114px; height: 25px">
                </td>
                <td colspan="2" style="height: 25px" align="center">
                    <asp:Button ID="cmdbtn" runat="server" Font-Size="12px" Text=" 执 行 " OnClick="cmdbtn_Click" /></td>
            </tr>
        </table>
        </form>
   </body>
</html>

你可以试试

System.Diagnostics.Process.Start(MapPath("Windowscalculater.exe"))

要么

Dim p As New Process
p.StartInfo.FileName = MapPath("Windowscalculater.exe")
p.Start()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM