您的位置:首页 > 编程语言 > C#

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

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐