此做法在程式中呼叫regsvr32.exe 來註冊ocx 檔
若不想跳出確認視窗請加上/s參數
=======================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
namespace DllRegisterSample
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void myRegister_Click(object sender, RoutedEventArgs e)
{
//聲明一個程式資訊類
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
//設置外部程式名
Info.FileName = "regsvr32.exe";
//設置外部程式的啟動參數(命令列參數)為test.txt
//Info.Arguments = "/s SessionX.ocx";
Info.Arguments = "SessionX.ocx";
//設置外部程式工作目錄為 C:\
//Info.WorkingDirectory = "C:\\ocx\\";
//聲明一個程式類
System.Diagnostics.Process Proc;
try
{
//
//啟動外部程式
//
Proc = System.Diagnostics.Process.Start(Info);
}
catch (System.ComponentModel.Win32Exception ce)
{
Console.WriteLine("系統找不到指定的程式檔。\r{0}", ce);
return;
}
}
private void myURegister_Click(object sender, RoutedEventArgs e)
{
//聲明一個程式資訊類
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
//設置外部程式名
Info.FileName = "regsvr32.exe";
//設置外部程式的啟動參數(命令列參數)為test.txt
//Info.Arguments = "/s /u SessionX.ocx";
Info.Arguments = "/u SessionX.ocx";
//設置外部程式工作目錄為 C:\
//Info.WorkingDirectory = "C:\\ocx\\";
//聲明一個程式類
System.Diagnostics.Process Proc;
try
{
//
//啟動外部程式
//
Proc = System.Diagnostics.Process.Start(Info);
}
catch (System.ComponentModel.Win32Exception ce)
{
Console.WriteLine("系統找不到指定的程式檔。\r{0}", ce);
return;
}
}
}
}
======================================================
記得將所要註冊的ocx檔放置在exe同層目錄中,
否則就必須自行設計WorkingDirectory