为了账号安全,请及时绑定邮箱和手机立即绑定

Silverlight调用本机exe程序

标签:
C#

要点:

1. Silverlight必须启用OOB模式,以及 Require elevated trust when running in-browser.参考下图设置

注:OOB模式,并不意味着必须脱离浏览器运行,仍然可以嵌入到网面中运行

 

2. 必须添加Microsoft.CSharp.dll 引用,默认在目录 c:\Program Files\Microsoft SDKs\Silverlight\v5.0\Libraries\Client\ 下

 

3. 调用的exe路径,不允许出现空格

下面是使用示例:

复制代码

 1 using System; 
 2 using System.Runtime.InteropServices.Automation; 
 3 using System.Windows; 
 4 using System.Windows.Controls; 
 5  
 6 namespace SLTrustSample 
 7 { 
 8     public partial class MainPage : UserControl 
 9     {
 10         public MainPage()
 11         {
 12             InitializeComponent();
 13         }
 14 
 15         private void CallExe(String exeFilePath)
 16         {
 17             try
 18             {
 19                 dynamic cmd = AutomationFactory.CreateObject("WScript.Shell");
 20                 cmd.Run(exeFilePath, 1, true);
 21             }
 22             catch (Exception ex)
 23             {
 24                 MessageBox.Show("权限不足,请再OOB模式下运行,详细原因:" + ex.Message);
 25                 return;
 26             }
 27         }
 28 
 29 
 30 
 31         private void LaunchMyApp(object sender, RoutedEventArgs e)
 32         {
 33             CallExe(@"C:\exe\cpu-monitor.exe");
 34         }
 35 
 36 
 37         private void LaunchNotepad(object sender, RoutedEventArgs e)
 38         {
 39             CallExe(@"notepad c:\\test.txt");
 40         }
 41     }
 42 }

复制代码

除调用本机exe外,其实还能干很多其它事情,比如读取Windows注册表,调用本机Com组件完成指定的功能...

 

使用场景:比如最常见的IM聊天工具,浏览网页后,在网页上点击“与我聊天”按钮,启动本机客户端的IM聊天工具。当然,不止这一种场景,其它场景请自行脑补:)

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消