using System.Management;ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach(ManagementObject mo in moc) { if((bool)mo["IPEnabled"] == true) Response.Write("MAC address\t\r"+ mo["MacAddress"].ToString()+" "); } ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); Response.Write("Volume Serial Number: " + disk.GetPropertyvalue("VolumeSerialNumber").ToString()); 参考文章 From: "Andy Cheung [MS]" <[email protected]> References: <uAnwrdo9BHA.1612@tkmsftngp07> <#qge8po9BHA.1868@tkmsftngp04> <eb9BgRr9BHA.2432@tkmsftngp04> Subject: Re: Access denied to WMI when using it from ASP .NET Web Services project. Date: Thu, 16 May 2002 19:09:57 -0700 Lines: 111 Organization: Microsoft X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Message-ID: <uEPXyfU$BHA.2296@tkmsftngp05> Newsgroups: microsoft.public.win32.programmer.wmi NNTP-Posting-Host: 131.107.3.75 The "Access Denied" error is returned by System.Management. It happens most likely when an ASP.NET web application tries to access System.Management with aspnet.exe worker process running under ASPNET account, which is a default in .NET RTM. There are two ways to work around the access problem you saw.1) Give <MachineNameHere>\ASPNET account remote-enable privilege to the namespace your web application is accessing (e.g. root\cimv2). To do that, run wmimgmt.msc. Right click "WMI Control" and select "Properties". Select the security tab and un-fold the namespace structure to find the namespace you want to allow your application to access. Click the security button and add ASPNET account. Check "Remote Enable" and click OK. NOTE: It is not recommened to grant 'Remote Enable' privilege to EVERYONE.2) Modify the username attribute in <processModel> tag in the machine.config file from "machine" to "system". This allows aspnet.exe worker process to run as local system (admin privilege) account. You need to restart IIS to let this take effect. NOTE: This workaround, however, is not recommended.This problem will be fixed by the upcoming service pack of Windows 2000 and Windows XP. -- Andy Cheung Microsoft WMI Test Engineer This posting is provided "As Is" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm "Jean-Francois Hamelin" <[email protected]> wrote in message news:eb9BgRr9BHA.2432@tkmsftngp04... > Event that way I have the same error. But if the same code is in a Windows > Application project type, it's execute with any problems. > > "Joakim Söderberg" <[email protected]> wrote in message > news:#qge8po9BHA.1868@tkmsftngp04... > > I have myself never used the ManagementScope.Connect() method. If you do > > something like the following you will achieve the same thing but you get to > > specify user credentials for connecting: > > > > ConnectionOptions options = new ConnectionOptions; > > options.Username = "<login name>"; > > options.Password = "<password>"; > > ManagementScope scope = new ManagementScope("\\\\root\\cimv2", options); > > ObjectQuery oq = new ObjectQuery("SELECT * FROM > > Win32_ComputerSystemProduct"); > > ManagementObjectSearcher query = new ManagementObjectSearcher(scope,oq); > > ManagementObjectCollection queryCollection = query.Get(); > > > > "Jean-Francois Hamelin" <[email protected]> wrote in > > message news:uAnwrdo9BHA.1612@tkmsftngp07... > > > Hi, > > > > > > I have created a ASP .NET Web Services to export some WMI information on a > > > W2K SP2 box. When connecting my ManagementScope object and exception is > > > raised with the following message: "Access Denied" when calling from the > > > service test page. > > > > > > Here's the code ( C# ). > > > > > > using System; > > > using System.Collections; > > > using System.ComponentModel; > > > using System.Data; > > > using System.Diagnostics; > > > using System.Web; > > > using System.Web.Services; > > > using System.Management; > > > > > > namespace Management > > > { > > > public class Service1 : System.Web.Services.WebService > > > { > > > public Service1() > > > { > > > InitializeComponent(); > > > } > > > > > > [WebMethod] > > > public string Connect() > > > { > > > ManagementScope scope = new ManagementScope("root\\CIMV2"); > > > try > > > { > > > scope.Connect(); > > > return "Success"; > > > } > > > catch( System.Exception e ) > > > { > > > return e.Message; > > > } > > > } > > > } > > > } > > > > > > Thanks > > > JF > > > > > > > > > > > >
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Response.Write("MAC address\t\r"+ mo["MacAddress"].ToString()+"
");
}
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get(); Response.Write("Volume Serial Number: " + disk.GetPropertyvalue("VolumeSerialNumber").ToString());
参考文章
From: "Andy Cheung [MS]" <[email protected]>
References: <uAnwrdo9BHA.1612@tkmsftngp07> <#qge8po9BHA.1868@tkmsftngp04> <eb9BgRr9BHA.2432@tkmsftngp04>
Subject: Re: Access denied to WMI when using it from ASP .NET Web Services project.
Date: Thu, 16 May 2002 19:09:57 -0700
Lines: 111
Organization: Microsoft
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Message-ID: <uEPXyfU$BHA.2296@tkmsftngp05>
Newsgroups: microsoft.public.win32.programmer.wmi
NNTP-Posting-Host: 131.107.3.75
The "Access Denied" error is returned by System.Management. It happens most
likely when an ASP.NET web application tries to access System.Management
with aspnet.exe worker process running under ASPNET account, which is a
default in .NET RTM. There are two ways to work around the access problem
you saw.1) Give <MachineNameHere>\ASPNET account remote-enable privilege to the
namespace your web application is accessing (e.g. root\cimv2).
To do that, run wmimgmt.msc. Right click "WMI Control" and select
"Properties". Select the security tab and un-fold the namespace structure to
find the namespace you want to allow your application to access. Click the
security button and add ASPNET account. Check "Remote Enable" and click OK.
NOTE: It is not recommened to grant 'Remote Enable' privilege to EVERYONE.2) Modify the username attribute in <processModel> tag in the machine.config
file from "machine" to "system". This allows aspnet.exe worker process to
run as local system (admin privilege) account. You need to restart IIS to
let this take effect.
NOTE: This workaround, however, is not recommended.This problem will be fixed by the upcoming service pack of Windows 2000 and
Windows XP.
--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Jean-Francois Hamelin" <[email protected]> wrote in
message news:eb9BgRr9BHA.2432@tkmsftngp04...
> Event that way I have the same error. But if the same code is in a Windows
> Application project type, it's execute with any problems.
>
> "Joakim Söderberg" <[email protected]> wrote in message
> news:#qge8po9BHA.1868@tkmsftngp04...
> > I have myself never used the ManagementScope.Connect() method. If you do
> > something like the following you will achieve the same thing but you get
to
> > specify user credentials for connecting:
> >
> > ConnectionOptions options = new ConnectionOptions;
> > options.Username = "<login name>";
> > options.Password = "<password>";
> > ManagementScope scope = new ManagementScope("\\\\root\\cimv2", options);
> > ObjectQuery oq = new ObjectQuery("SELECT * FROM
> > Win32_ComputerSystemProduct");
> > ManagementObjectSearcher query = new ManagementObjectSearcher(scope,oq);
> > ManagementObjectCollection queryCollection = query.Get();
> >
> > "Jean-Francois Hamelin" <[email protected]> wrote in
> > message news:uAnwrdo9BHA.1612@tkmsftngp07...
> > > Hi,
> > >
> > > I have created a ASP .NET Web Services to export some WMI information
on
a
> > > W2K SP2 box. When connecting my ManagementScope object and exception
is
> > > raised with the following message: "Access Denied" when calling from
the
> > > service test page.
> > >
> > > Here's the code ( C# ).
> > >
> > > using System;
> > > using System.Collections;
> > > using System.ComponentModel;
> > > using System.Data;
> > > using System.Diagnostics;
> > > using System.Web;
> > > using System.Web.Services;
> > > using System.Management;
> > >
> > > namespace Management
> > > {
> > > public class Service1 : System.Web.Services.WebService
> > > {
> > > public Service1()
> > > {
> > > InitializeComponent();
> > > }
> > >
> > > [WebMethod]
> > > public string Connect()
> > > {
> > > ManagementScope scope = new ManagementScope("root\\CIMV2");
> > > try
> > > {
> > > scope.Connect();
> > > return "Success";
> > > }
> > > catch( System.Exception e )
> > > {
> > > return e.Message;
> > > }
> > > }
> > > }
> > > }
> > >
> > > Thanks
> > > JF
> > >
> > >
> >
> >
>
>