简体   繁体   中英

How do I execute a .bat stored on a server via a web interface?

All,

Forgive the lack of sample code here. I have a .bat file which executes a background process that refreshes graphics on a webpage that I am designing. I can run this process automatically using a scheduling application, but I would like to give the user manual control. I want a button on a webpage that the user can click to run a .bat file on the server. I'm coding in html and C# on an aspx file.

What I've tried so far is:

<a href='program.bat'> Refresh </a> 

Obviously this doesn't work: when I click "refresh" on my IE browser and select "run" the file downloads to the temporary files folder on my local drive and tries to execute there.

If you want the batch file to run on the server, then you will have to call it from your C# code, not in the html.

There are many ways to do that. The simplest is to have an aspx page that calls it from OnLoad , let's say update.aspx:

<script runat="server">
    protected override void OnLoad( EventArgs e )
    {
        System.Diagnostics.Process.Start( @"c:\path\to\program.bat" );
    }
</script>

Then your html has a regular <a href="update.aspx"> tag.

Of course it can also be done as a postback on a button press, etc. The point is it's done on the server side, in some codebehind.

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