首页 新闻 赞助 找找看

C#程序调用外部程序--

0
悬赏园豆:15 [已解决问题] 解决于 2008-05-15 18:36
<P>我想单击一个按钮然后 自己弹出来一个记事本.用到C#程序调用外部程序.老师说从网上搜一下,然后我搜到的是</P> <P>/*<BR>*&nbsp; 编程语言:Visual&nbsp; Studio&nbsp; .NET&nbsp; C#&nbsp; (Beta&nbsp; 2)<BR>*&nbsp; 作&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 者:迪泊威<BR>*&nbsp; 功&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 能:通过C#程序调用&nbsp; Windows&nbsp; 记事本程序&nbsp; 编辑一个<BR>*&nbsp; 名为&nbsp; test.txt&nbsp; 的文本文件。<BR>*<BR>*&nbsp; 在整个程序中&nbsp; System.Diagnostics.Process.Start(Info)&nbsp; <BR>*&nbsp; 为主要语句。<BR>*&nbsp; 如果只是单独执行一个外部程序,可用一条如下代码即可:<BR>*&nbsp; System.Diagnostics.Process.Start(<BR>*&nbsp; "外部程序名","启动参数");<BR>*/</P> <P>using&nbsp; System;</P> <P>class&nbsp; test<BR>{<BR>static&nbsp; void&nbsp; Main()<BR>{</P> <P>//声明一个程序信息类<BR>System.Diagnostics.ProcessStartInfo&nbsp; Info&nbsp; =&nbsp; new&nbsp; System.Diagnostics.ProcessStartInfo();</P> <P>//设置外部程序名<BR>Info.FileName&nbsp; =&nbsp; "notepad.exe";</P> <P>//设置外部程序的启动参数(命令行参数)为test.txt<BR>Info.Arguments&nbsp; =&nbsp; "test.txt";</P> <P>//设置外部程序工作目录为&nbsp; C:\<BR>Info.WorkingDirectory&nbsp; =&nbsp; "C:\\";</P> <P>//声明一个程序类<BR>System.Diagnostics.Process&nbsp; Proc&nbsp; ;</P> <P>try<BR>{<BR>//<BR>//启动外部程序<BR>//<BR>Proc&nbsp; =&nbsp; System.Diagnostics.Process.Start(Info);<BR>}<BR>catch(System.ComponentModel.Win32Exception&nbsp; e)<BR>{<BR>Console.WriteLine("系统找不到指定的程序文件。\r{0}",&nbsp; e);<BR>return;<BR>}</P> <P>//打印出外部程序的开始执行时间<BR>Console.WriteLine("外部程序的开始执行时间:{0}",&nbsp; Proc.StartTime);</P> <P>//等待3秒钟<BR>Proc.WaitForExit(3000);</P> <P>//如果这个外部程序没有结束运行则对其强行终止<BR>if(Proc.HasExited&nbsp; ==&nbsp; false)<BR>{<BR>Console.WriteLine("由主程序强行终止外部程序的运行!");<BR>Proc.Kill();<BR>}<BR>else<BR>{<BR>Console.WriteLine("由外部程序正常退出!");<BR>}<BR>Console.WriteLine("外部程序的结束运行时间:{0}",&nbsp; Proc.ExitTime);<BR>Console.WriteLine("外部程序在结束运行时的返回值:{0}",&nbsp; Proc.ExitCode);<BR>}<BR>} </P> <P>&nbsp;</P> <P>我把这段代码粘到按钮的 Click 事件中!然后运行报错!F:\Working\WindowsApplication1\frmMain.cs(137,15): 错误 CS1513: 应输入 }<BR>F:\Working\WindowsApplication1\frmMain.cs(199,1): 错误 CS1022: 应输入类型、命名空间定义或文件尾</P> <P>&nbsp;</P> <P>我是初学者,好多地方还不是太懂.万分感谢各位朋友的帮助!!</P>
问题补充: 请问 我该怎么写这段代码呢? 就是单击一个按钮,然后自己弹出一个记事本? 我要是单击一下,弹出一个计算器.代码该怎么写呢?
junli2201的主页 junli2201 | 初学一级 | 园豆:140
提问于:2008-05-15 14:23
< >
分享
最佳答案
0
在按钮的单击事件中写下这句代码就足够了. System.Diagnostics.Process.Start("notepad.exe");
侯垒 | 老鸟四级 |园豆:3435 | 2008-05-15 14:47
其他回答(3)
0
ProcessStartInfo startInfo = new ProcessStartInfo("Notepad.exe"); startInfo.WindowStyle = ProcessWindowStyle.Normal; startInfo.Arguments = AppUtil.PatchPath + "/Log/Log.txt"; try { Process.Start(startInfo); } catch { return; } 以上路径换成自己的路径 即可
李.net | 园豆:730 (小虾三级) | 2008-05-15 15:22
0
看不懂你的错误信息,简单比较了下,花括号好象是匹配的。
无之无 | 园豆:5095 (大侠五级) | 2008-05-15 17:01
0
计算器的是System.Diagnostics.Process.Start("calc.exe");
wingoo | 园豆:1513 (小虾三级) | 2008-05-15 18:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册