如何运行EXE文件ASP.net?

11dmarpk  于 2022-11-19  发布在  .NET
关注(0)|答案(4)|浏览(217)

我用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);

我从编译器执行,它运行正常,但当我发布我的项目在localhost我没有任何行动。什么问题?

qojgxg4l

qojgxg4l1#

您尝试了什么操作?可能是权限问题。默认情况下,您的网站不应执行外部可执行文件,因为这将带来严重的安全风险。也许有其他解决方案可以解决您的问题,但不涉及运行外部程序?
无论哪种方式,我强烈建议不要直接从你的网站运行可执行文件。如果你真的必须运行程序,也许你可以写一个简单的Windows服务,它可以接收消息(通过WCF?),然后在一个隔离的环境中执行你的程序。

e0bqpujr

e0bqpujr2#

为了执行(运行)www.example.com中的应用程序asp.net,我使用了以下方法:
1)请参阅其他网站上的代码:


2)在web.config的“assemblyIdentity”字段中,我添加了“模拟”参数,如下所示:
更改自

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

<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35" impersonate="true" userName="myuser" password="123123"/>
  • (首先,您可能需要设置该文件夹的sql/windows用户和密码权限)。*

2)然后我在我的www.example.com中使用了该命令asp.net(可能也使用了asp2.0):

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

pbgvytdp3#

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

<!-- 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>
gv8xihay

gv8xihay4#

你可以试试

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

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

相关问题