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;
        }
    }
  • 相关阅读:
    js里面的 InttoStr 和 StrtoInt
    预编译知识 (转载记录)
    C语言操作内存
    C语言操作文件
    C语言
    如何调试shell脚本
    设计模式-装饰者模式
    自己动手制作一个模版解析
    设计模式-单例模式
    http中关于缓存的那些header信息
  • 原文地址:https://www.cnblogs.com/justin_wh/p/2741466.html
Copyright © 2011-2022 走看看