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;
}
}