私は、wmiを通じて公開された のクラスを持つ別個のアセンブリを作成しました。次に、 がアセンブリを参照し、クラスを公開しようとするWindowsフォームアプリケーションを作成しました。 クラスを公開しようとすると、タイプ System.Management.Instrumentation.WmiProviderInstallationExceptionの例外が発生します。例外のメッセージ は、 " タイプの例外 'System.Management.Instrumentation.WMIInfraException'がスローされました。"私には の意味がありません。私は試してみました.Net2.0と.Net3.5(sp1も)と同じ結果を得る。wmiクラスを.netで公開するには?
以下は私のwmiクラスです。その後、私が公開したコードが続きます。
//Interface.cs in assembly WMI.Interface.dll
using System;
using System.Collections.Generic;
using System.Text;
[assembly: System.Management.Instrumentation.WmiConfiguration(@"root\Test",
HostingModel =
System.Management.Instrumentation.ManagementHostingModel.Decoupled)]
namespace WMI
{
[System.ComponentModel.RunInstaller(true)]
public class MyApplicationManagementInstaller :
System.Management.Instrumentation.DefaultManagementInstaller { }
[System.Management.Instrumentation.ManagementEntity(Singleton = true)]
[System.Management.Instrumentation.ManagementQualifier("Description",
Value = "Obtain processor information.")]
public class Interface
{
[System.Management.Instrumentation.ManagementBind]
public Interface()
{
}
[System.Management.Instrumentation.ManagementProbe]
[System.Management.Instrumentation.ManagementQualifier("Descriiption",
Value="The number of processors.")]
public int ProcessorCount
{
get { return Environment.ProcessorCount; }
}
}
}
//Button click in windows forms application to publish class
try
{
System.Management.Instrumentation.InstrumentationManager.Publish(new
WMI.Interface());
}
catch (System.Management.Instrumentation.InstrumentationException
exInstrumentation)
{
MessageBox.Show(exInstrumentation.ToString());
}
catch (System.Management.Instrumentation.WmiProviderInstallationException
exProvider)
{
MessageBox.Show(exProvider.ToString());
}
catch (Exception exPublish)
{
MessageBox.Show(exPublish.ToString());
}