您的位置:首页 > 编程语言 > ASP

ASP.NET、C#调用外部可执行exe文件--多种方案

2014-05-07 11:56 666 查看
一、

try
{
//方法一
//调用自己的exe传递参数
//Process proc = new Process();
//proc.StartInfo.FileName = @"D:\\hotelsoft\\zk.exe";
//proc.Start();
//Thread.Sleep(3000);//暂停3秒
//foreach (System.Diagnostics.Process pro in

System.Diagnostics.Process.GetProcessesByName("zk"))
//{
//  查找并关闭进程
//    pro.Kill();
//}

//方法二
ProcessStartInfo startInfo = new ProcessStartInfo(@"D:\\hotelsoft\\zk.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;

Process.Start(startInfo);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}


二、

//调用外部程序导cmd命令行
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
/ /向cmd.exe输入command
p.StandardInput.WriteLine("cd C:\\Inetpub\\wwwroot\\Paicdom\\PaWebService\\PaWeb\\Manage\\Exportcsv");
//cmd又调用了ociuldr.ex
p.StandardInput.WriteLine("ociuldr.exe");
p.StandardInput.WriteLine("exit"); //需要有这句,不然程序会挂机
//string output = p.StandardOutput.ReadToEnd(); 这句可以用来获取执行命令的输出结果

我在ASP.NET中调用,一直想看看到执行窗口,但无论怎么设置参数,都看不到。不知如何。

三、

C#程序作为调用这需要生成一个Progress类,该类提供了调用EXE可执行文件所用到的属性和事件.System.Diagnostics.Process pExecuteEXE = new System.Diagnostics.Process();
pExecuteEXE.StartInfo.FileName = @"E:\Delphi.exe";
pExecuteEXE.StartInfo.Arguments = "'paramstr1 paramstr2,paramstr3'";
pExecuteEXE.Start();
pExecuteEXE.WaitForExit();//无限期等待完成
//pExecuteEXE.WaitForExit(10000);//等待最长10秒钟完成。Delphi可执行程序作为被调用程序,主要是接收参数信息,执行程序,由于程序执行程序完毕不能返回给调用程序信息,只能将信息写入某一位置等待调用者读取。
procedure TForm1.Button1Click(Sender: TObject);

var
i:integer;
begin
self.Caption :='';
for i:=0 to paramcount do
begin
self.Caption :=self.Caption+ '['+inttostr(i)+':'+paramstr(i)+']';
end;

end;

完成后退出。

这里是一个简单的调用例子,可以效仿:
* 功 能:通过C#程序调用 Windows 记事本程序 编辑一个
* 名为 test.txt 的文本文件。
*
* 在整个程序中 System.Diagnostics.Process.Start(Info)
* 为主要语句。
* 如果只是单独执行一个外部程序,可用一条如下代码即可:
* System.Diagnostics.Process.Start(
* "外部程序名","启动参数");
*/

using System;

class test
{
static void Main()
{

//声明一个程序信息类
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();

//设置外部程序名
Info.FileName = "notepad.exe";

//设置外部程序的启动参数(命令行参数)为test.txt
Info.Arguments = "test.txt";

//设置外部程序工作目录为 C:\
Info.WorkingDirectory = "C:\\";

//声明一个程序类
System.Diagnostics.Process Proc ;

try
{
//
//启动外部程序
//
Proc = System.Diagnostics.Process.Start(Info);
}
catch(System.ComponentModel.Win32Exception e)
{
Console.WriteLine("系统找不到指定的程序文件。\r{0}", e);
return;
}

//打印出外部程序的开始执行时间
Console.WriteLine("外部程序的开始执行时间:{0}", Proc.StartTime);

//等待3秒钟
Proc.WaitForExit(3000);

//如果这个外部程序没有结束运行则对其强行终止
if(Proc.HasExited == false)
{
Console.WriteLine("由主程序强行终止外部程序的运行!");
Proc.Kill();
}
else
{
Console.WriteLine("由外部程序正常退出!");
}
Console.WriteLine("外部程序的结束运行时间:{0}", Proc.ExitTime);
Console.WriteLine("外部程序在结束运行时的返回值:{0}", Proc.ExitCode);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐