COMAdmin 管理COM+ C# 抄自 CODEGURU 很多未知,整理一下
2013-03-28 14:46
561 查看
抄自
http://www.codeguru.com/cpp/com-tech/complus/article.php/c3943/COM-Automation-Using-COMAdminCatalog-in-NET-C.htm
但是发现很多未知引用,只是整理了一下作参考
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using COMAdmin;
namespace updatebh
{
class Comadminclass
{
private COMAdminCatalog objAdmin;
private object objRoot;
private COMAdminCatalogCollection objCollection ;
private NameValueCollection collection;
public delegate void DeleteEventHandler();
public delegate void ErrorMessageHandler(string s, Exception e);
//Events
/// <summary>Raised when the object is successfully deleted on
/// the remote host.</summary>
public event DeleteEventHandler Delete;
/// <summary>Raised when the object throws an exception.</summary>
public event ErrorMessageHandler Error;
//Method(s) to invoke the event
protected virtual void OnDelete()
{
if(Delete != null)
Delete();
}
//#region Get COM + Applications
// Get COM + Applications
internal COMAdminCatalogCollection GetCollection()
{
try
{
objAdmin = new COMAdmin.COMAdminCatalog();
objRoot = objAdmin.Connect("localhost");
objCollection = (COMAdmin.COMAdminCatalogCollection)objAdmin.GetCollection("Applications");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error : " + ex);
}
return objCollection;
}
public NameValueCollection GetCOMApplications()
{
collection = new NameValueCollection();
try
{
objCollection = GetCollection();
objCollection.Populate();
foreach (COMAdmin.COMAdminCatalogObject objAppNames
in objCollection)
{
COMAdmin.ICatalogCollection objComponents =
(COMAdmin.ICatalogCollection)objCollection.
GetCollection("Components", objAppNames.Key);
objComponents.Populate();
foreach (COMAdmin.COMAdminCatalogObject Components
in objComponents)
{
collection.Add(Components.Name.ToString(),
objAppNames.Name.ToString());
}
}
}
catch (Exception e)
{
Error += new ErrorMessageHandler(Message);
Error("GetCOMAPPlications", e);
Dispose();
}
return collection;
}
internal void Message(string message, Exception e)
{
System.Windows.Forms.MessageBox.Show("Error occurred " +
message + "error description : " + e, "Nexsure COM + ", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
public void DeleteCOMApplication(string appName)
{
try
{
long l_Count = 0;
ICatalogCollection pCollection = GetCollection();
ICatalogObject pCatalog;
pCollection.Populate();
l_Count = pCollection.Count;
if (l_Count == 0)
return;
for (int i = 0; i < l_Count; i++)
{
pCatalog = (ICatalogObject)pCollection.get_Item(i);
if (appName == (string)pCollection.get_Value("Name"))
{
pCollection.Remove(i);
pCollection.SaveChanges();
OnDelete();
return;
}
}
}
catch (Exception e)
{
Error += new ErrorMessageHandler(Message);
Error("Unable to delete the COM+ Application:", e);
Dispose();
}
}
public void CreateCOMApplication()
{
bool Exists = false;
try
{
Exists = ApplicationExists(); // Method to check whether
// the application exists
// in the Catalog
if (!Exists)
{
objCollection = GetCollection();
COMAdmin.COMAdminCatalogObject objObject = (COMAdmin.COMAdminCatalogObject)objCollection.Add();
objObject.set_Value("Name", props.COM_Name);
objObject.set_Value("Description", props.COM_Description);
objObject.set_Value("Activation", props.COM_Type);
objObject.set_Value("Identity", props.COM_User);
objCollection.SaveChanges();
OnCreate();
}
else
{
System.Windows.Forms.MessageBox.Show("An application with the same name already exixts in the Registry!", "Nexsure COM + Help", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
Error += new ErrorMessageHandler(Message);
Error("Error creating COM+ Application :", ex);
Dispose();
}
}
public bool StartCOMApplication()
{
bool Startup = false;
try
{
objCollection = GetCollection(); // Method returns
// COM+ Applications
objAdmin.StartApplication(applicationName);
System.Windows.Forms.MessageBox.Show("Application was successfully started.");
Startup = true;
}
catch
{
System.Windows.Forms.MessageBox.Show("Unable to start the application.");
}
return Startup;
}
public bool ShutDownCOMApplication()
{
bool ShuttingDown = false;
try
{
objCollection = GetCollection();
objAdmin.ShutdownApplication(applicationName);
System.Windows.Forms.MessageBox.Show("Application was successfully shut down.");
ShuttingDown = true;
}
catch
{
System.Windows.Forms.MessageBox.Show("Unable to shut down the application.");
}
return ShuttingDown;
}
}
}
http://www.codeguru.com/cpp/com-tech/complus/article.php/c3943/COM-Automation-Using-COMAdminCatalog-in-NET-C.htm
但是发现很多未知引用,只是整理了一下作参考
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using COMAdmin;
namespace updatebh
{
class Comadminclass
{
private COMAdminCatalog objAdmin;
private object objRoot;
private COMAdminCatalogCollection objCollection ;
private NameValueCollection collection;
public delegate void DeleteEventHandler();
public delegate void ErrorMessageHandler(string s, Exception e);
//Events
/// <summary>Raised when the object is successfully deleted on
/// the remote host.</summary>
public event DeleteEventHandler Delete;
/// <summary>Raised when the object throws an exception.</summary>
public event ErrorMessageHandler Error;
//Method(s) to invoke the event
protected virtual void OnDelete()
{
if(Delete != null)
Delete();
}
//#region Get COM + Applications
// Get COM + Applications
internal COMAdminCatalogCollection GetCollection()
{
try
{
objAdmin = new COMAdmin.COMAdminCatalog();
objRoot = objAdmin.Connect("localhost");
objCollection = (COMAdmin.COMAdminCatalogCollection)objAdmin.GetCollection("Applications");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error : " + ex);
}
return objCollection;
}
public NameValueCollection GetCOMApplications()
{
collection = new NameValueCollection();
try
{
objCollection = GetCollection();
objCollection.Populate();
foreach (COMAdmin.COMAdminCatalogObject objAppNames
in objCollection)
{
COMAdmin.ICatalogCollection objComponents =
(COMAdmin.ICatalogCollection)objCollection.
GetCollection("Components", objAppNames.Key);
objComponents.Populate();
foreach (COMAdmin.COMAdminCatalogObject Components
in objComponents)
{
collection.Add(Components.Name.ToString(),
objAppNames.Name.ToString());
}
}
}
catch (Exception e)
{
Error += new ErrorMessageHandler(Message);
Error("GetCOMAPPlications", e);
Dispose();
}
return collection;
}
internal void Message(string message, Exception e)
{
System.Windows.Forms.MessageBox.Show("Error occurred " +
message + "error description : " + e, "Nexsure COM + ", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
public void DeleteCOMApplication(string appName)
{
try
{
long l_Count = 0;
ICatalogCollection pCollection = GetCollection();
ICatalogObject pCatalog;
pCollection.Populate();
l_Count = pCollection.Count;
if (l_Count == 0)
return;
for (int i = 0; i < l_Count; i++)
{
pCatalog = (ICatalogObject)pCollection.get_Item(i);
if (appName == (string)pCollection.get_Value("Name"))
{
pCollection.Remove(i);
pCollection.SaveChanges();
OnDelete();
return;
}
}
}
catch (Exception e)
{
Error += new ErrorMessageHandler(Message);
Error("Unable to delete the COM+ Application:", e);
Dispose();
}
}
public void CreateCOMApplication()
{
bool Exists = false;
try
{
Exists = ApplicationExists(); // Method to check whether
// the application exists
// in the Catalog
if (!Exists)
{
objCollection = GetCollection();
COMAdmin.COMAdminCatalogObject objObject = (COMAdmin.COMAdminCatalogObject)objCollection.Add();
objObject.set_Value("Name", props.COM_Name);
objObject.set_Value("Description", props.COM_Description);
objObject.set_Value("Activation", props.COM_Type);
objObject.set_Value("Identity", props.COM_User);
objCollection.SaveChanges();
OnCreate();
}
else
{
System.Windows.Forms.MessageBox.Show("An application with the same name already exixts in the Registry!", "Nexsure COM + Help", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
Error += new ErrorMessageHandler(Message);
Error("Error creating COM+ Application :", ex);
Dispose();
}
}
public bool StartCOMApplication()
{
bool Startup = false;
try
{
objCollection = GetCollection(); // Method returns
// COM+ Applications
objAdmin.StartApplication(applicationName);
System.Windows.Forms.MessageBox.Show("Application was successfully started.");
Startup = true;
}
catch
{
System.Windows.Forms.MessageBox.Show("Unable to start the application.");
}
return Startup;
}
public bool ShutDownCOMApplication()
{
bool ShuttingDown = false;
try
{
objCollection = GetCollection();
objAdmin.ShutdownApplication(applicationName);
System.Windows.Forms.MessageBox.Show("Application was successfully shut down.");
ShuttingDown = true;
}
catch
{
System.Windows.Forms.MessageBox.Show("Unable to shut down the application.");
}
return ShuttingDown;
}
}
}
相关文章推荐
- 今天整理了论坛很多关于dotnet(C#)的试题,大家共享一下
- 整理了论坛很多关于dotnet(C#)的试题,大家共享一下
- 今天整理了论坛很多关于dotnet(C#)的试题,大家共享一下
- 做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法。 很方便的,于是就简单的整理了一下。 1.1 showMessageDialog 显示一个带有OK 按钮的模态
- 【整理pcnetman888帖子】奇怪的局部变量:讨论一下C#中的闭包【关于闭包再续】
- 关于c#的config配置文件操作网上很多讲的和实际运用感觉不符,终于理解了一点点,记录一下
- 位图排序 C语言版本,C#版本,javascript版本(小整理了一下)
- 在.NET中使用COMAdmin组件管理COM+组件
- 以前写的一个手机通讯录管理小程序回忆一下(C#版大牛绕过)致曾经写过的小程序
- 最近总结了很多C#以及Unity的基础知识,分享一下
- 最近准备整理一下手头资料,开发一个工作流和表单管理系统
- 发现很多有趣的东西,需要整理一下了
- 之前的gdi+绘图,整理了一下,清晰了很多
- 【转及整理】C#管理快捷方式文件创建
- SQL2008R2 导出表 成dbf文件,网上很多文档,但是真正能执行生成dbf文件的不多,我整理了一下:
- 整理一下java5的新特性(引用了很多,都有原文链接)
- C# 通过API获得屏幕某点的颜色(先转别人代码,有时间整理一下)
- 有关C#中的文件操作之目录管理 个人整理
- 魔方教程--很多教程都不对,整理并实践了一下
- C#正则表达式经典分类整理集合手册