简体   繁体   中英

how to Run EXE file By ASP.net?

I created a page for ASP.NET in C# to run an exe file. I created a button and an event with the below code:

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

I executed from compiler and it ran normally, but when I published my project in localhost I had no action. What is the problem?

What did you try? It's probably a problem with rights. By default, your website is not supposed to be able to execute external executables, because this would be a serious security risk. Maybe there is a different solution to your problem which does not involve running an external program?

Either way, I would strongly advise against running an executable directly from your website. If you really have to run the program, maybe you can write a simple windows service which can receive a message (over WCF?), which will then execute your program in an isolated environment.

to execute(run) applications in asp.net, i used this method:

1) see the codes on other sites:

or

2) in web.config ,in " assemblyIdentity " field, i have added "impersonate" parameters ,like this:

changed from

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

to

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

(at first, you may need to set up sql/windows user and password permisions to that folder).

2) then i used the command in my asp.net (maybe with asp 2.0 too):

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

Below is my example code, Get user input and execute an executable file.

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

====================== another example ==========================

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

You can try

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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