zoukankan      html  css  js  c++  java
  • silverlight中写Com组件 实现打开本地文件功能

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Diagnostics;
    
    
    namespace OpenFileCom
    {
        /// <summary>
        /// 事件接口
        /// </summary>
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IComEvents
        {
            /// <summary>
            /// 定义要暴露的事件
            /// </summary>
            void OnHelloBegin(object sender, EventArgs e);
        }
    
        public class HelloEvents : EventArgs
        { }
    
        /// <summary>
        /// 自定义定义委托
        /// </summary>
        public delegate void Comdel(object sender, EventArgs e);
    
        [ComVisible(true)]
        [ComSourceInterfaces(typeof(IComEvents))]
        public class IMDCOMTEST
        {
            public string Hello()
            {
                if (OnHelloBegin != null)
                    OnHelloBegin(this, new HelloEvents());
                return "asdasdasd";
            }
            public string OpenFile(string _path)
            {
    
                // var extensionName = Path.GetExtension(_path);
                try
                {
                    if (!File.Exists(_path))
                        throw new FileNotFoundException();
                    Process p = new Process();
                    p.StartInfo.FileName = "cmd.exe";           //設定程序名
                    p.StartInfo.Arguments = string.Format("/c \"{0}\"", _path);    //設定程式執行參數
                    p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
                    p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入
                    p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出
                    p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
                    p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口
                    return p.Start() ? "" : "打开失败";   //啟動                 
                }
                catch (Exception ex) { return ex.Message; }
            }
    
            /// <summary>
            /// 实现事件接口
            /// </summary>
            public event Comdel OnHelloBegin;
        }
    }
  • 相关阅读:
    转载 listview的一种常用布局
    转载 Android gallery实现图片的左右循环旋转
    转载 android 所有布局属性和UI控件
    转载 android 支持展开/收缩功能的列表控件
    svn实现文件/目录的共享
    linux 下命令行修改IP地址
    oracle 10g数据库闪回配置与使用
    Oracle数据库实例启动不了怎么办?
    oracle 权限列表
    apache 配置虚拟目录
  • 原文地址:https://www.cnblogs.com/justin_wh/p/2741466.html
Copyright © 2011-2022 走看看