asp.net调用exe并传递参数然后关闭exe

先用C#写个简单的exe,这里我就用winForm
Program.cs这里加了个启动参数
ExpandedBlockStart.gif 大气象
using  System;
using  System.Collections.Generic;
using  System.Windows.Forms;

namespace  WindowsApplication1
{
    
static   class  Program
    {
        
///   <summary>
        
///  应用程序的主入口点。
        
///   </summary>
        [STAThread]
        
static   void  Main( string [] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(
false );
            Application.Run(
new  Form1(args));
        }
    }
}
Form1.cs
ExpandedBlockStart.gif 大气象
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  WindowsApplication1
{
    
public   partial   class  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
        }
        
public  Form1( string [] s)
        {
            InitializeComponent();
            
if  (s.Length  >   0 )
                MessageBox.Show(s[
0 ]);
            
if  (s.Length  >   1 )
                MessageBox.Show(s[
1 ]);
        }
    }
}


然后建个网站项目。
ExpandedBlockStart.gif 大气象
protected   void  btnCount_Click( object  sender, EventArgs e)
{
    
// 调用记事本
    
// System.Diagnostics.Process.Start("C:\\WINDOWS\\system32\\notepad.exe"); 
     try
    {
        
// 调用自己的exe传递参数
        Process proc  =   new  Process();
        
string  sPath  =  Request.MapPath( " ~ " ); // 取得物理路径
        proc.StartInfo.FileName  =  sPath  +   @" \hi.exe " ;
        proc.StartInfo.Arguments 
=   " 参数1 参数2 " ;
        proc.Start();

        Thread.Sleep(
5000 ); // 暂停3秒

        
foreach  (System.Diagnostics.Process pro  in  System.Diagnostics.Process.GetProcessesByName( " hi " ))
        {
            pro.Kill();
        }
    }
    
catch  (Exception ex)
    {
        Response.Write(ex.ToString());
    }
}

这里exe的打开与关闭都有了。
这种方法直接用vs打开可以正常运行,但是放在iis下就不行了,权限不足。
最后找到一个方法就是通过Windows API调用外部程序
同样写个简单的控制台程序。就是在D盘新建一个文本文件。

ExpandedBlockStart.gif 大气象
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.IO;
namespace  ConsoleApplication1
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            FileInfo f 
=   new  FileInfo( @" D:\hi.txt " );
            StreamWriter w 
=  f.CreateText();
            
string  s  =   " 0 " ;
            
if  (args.Length  >   0 )
                s 
=   " 123 " ;
            w.WriteLine(
" dddd "   +  s);
            w.Close();

        }
    }
}

 

通过Web Service调用

ExpandedBlockStart.gif 大气象
using  System;
using  System.Web;
using  System.Collections;
using  System.Web.Services;
using  System.Web.Services.Protocols;

using  System.Diagnostics;
using  System.ComponentModel;
using  System.Runtime.InteropServices;
using  System.Text;

///   <summary>
///  WebService 的摘要说明
///   </summary>
[WebService(Namespace  =   " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
public   class  WebService : System.Web.Services.WebService
{
    [DllImport(
" shell32.dll  " )]
    
public   static   extern   int  ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir,  int  FsShowCmd);

    
public  WebService()
    {

        
// 如果使用设计的组件,请取消注释以下行 
        
// InitializeComponent(); 
    }

    [WebMethod]
    
public   string  HelloWorld()
    {
        ShellExecute(IntPtr.Zero, 
new  StringBuilder( " Open " ),  new  StringBuilder( " hi " ),  new  StringBuilder( " jjj " ),  new  StringBuilder( @" D:\ " ),  1 );

        
return   " Hello World " ;
    }

}

 

这是目前asp.net调用exe找到的正确运行的方法。放在IIS中正确运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值