本文整理汇总了C#中System.Management.ManagementClass.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# ManagementClass.Dispose方法的具体用法?C# ManagementClass.Dispose怎么用?C# ManagementClass.Dispose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.ManagementClass
的用法示例。
在下文中一共展示了ManagementClass.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetOsVersion
// Windowsの製品名を取得
public static string GetOsVersion()
{
string OsVersion = null;
ManagementClass objCS = new ManagementClass("Win32_OperatingSystem");
ManagementObjectCollection objCol = objCS.GetInstances();
foreach(ManagementObject objMgmt in objCol)
{
OsVersion = objMgmt["Caption"].ToString();
objMgmt.Dispose();
}
objCol.Dispose();
objCS.Dispose();
if (System.Environment.Is64BitOperatingSystem)
{
return OsVersion + "(64bit)";
}
else
{
return OsVersion + "(32bit)";
}
}
示例2: GetMemory
private float GetMemory()
{
float capacity = 0;
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo1 in moc1)
{
capacity += (float)Convert.ToDouble(mo1.Properties["Capacity"].Value);
}
moc1.Dispose();
cimobject1.Dispose();
return capacity;
}
示例3: getDiskDriveid
/// <summary>
/// 获取硬盘ID
/// </summary>
/// <returns></returns>
public static string getDiskDriveid()
{
//获取硬盘ID
String HDid = "";
ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
break;
}
cimobject1.Dispose();
moc1.Dispose();
return HDid;
}
示例4: GetCpuCore
public static string GetCpuCore()
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection cpus = mc.GetInstances();
string count = null;
string co = null;
ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
foreach (ManagementObject mo in mySearch.Get())
{
count = mo.Properties["NumberOfCores"].Value.ToString();
}
mc.Dispose();
mySearch.Dispose();
return count;
}
示例5: GetMetaInformation
public ManagementObject GetMetaInformation(string name)
{
ManagementObject value = null;
ManagementScope scope = new ManagementScope(@"\\localhost");
ManagementPath path = new ManagementPath(name);
ManagementClass management = new ManagementClass(scope, path, null);
foreach (ManagementObject child in management.GetInstances())
{
value = child;
break;
}
management.Dispose();
return value;
}
示例6: GetCpuMHZ
/// <summary>
/// 获取CPU的频率 这里之所以使用string类型的数组,主要是因为cpu的多核
/// </summary>
/// <returns></returns>
public static string[] GetCpuMHZ()
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection cpus = mc.GetInstances();
string[] mHz = new string[cpus.Count];
int c = 0;
ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
foreach (ManagementObject mo in mySearch.Get())
{
mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
c++;
}
mc.Dispose();
mySearch.Dispose();
return mHz;
}
示例7: GetPhysicalMemorySize
/// <summary>
/// 获取物理内存大小
/// </summary>
/// <returns></returns>
public static ulong GetPhysicalMemorySize()
{
ulong PhysicalMemorySize = 0, FreePhysicalMemory = 0;
ManagementClass osClass = new ManagementClass("Win32_OperatingSystem");
foreach (ManagementObject obj in osClass.GetInstances())
{
if (obj["TotalVisibleMemorySize"] != null)
PhysicalMemorySize = (ulong)obj["TotalVisibleMemorySize"];
if (obj["FreePhysicalMemory"] != null)
FreePhysicalMemory = (ulong)obj["FreePhysicalMemory"];
break;
}
osClass.Dispose();
return PhysicalMemorySize;
}
示例8: sys_FrameWorkInfoTable
/// <summary>
/// 构造函数
/// </summary>
public sys_FrameWorkInfoTable()
{
ManagementClass mc = new ManagementClass("Win32_Processor");
try
{
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
_S_RegsionGUID = mo.Properties["ProcessorId"].Value.ToString();
break;
}
}
catch (Exception ex)
{
_S_RegsionGUID = Common.EnBase64(AppDomain.CurrentDomain.BaseDirectory);
FileTxtLogs.WriteServiceLog(ex.ToString(), "LoadFrameWorkInfoTable");
}
finally {
mc.Dispose();
}
}
示例9: buttonAccept_Click
private void buttonAccept_Click(object sender, EventArgs e)
{
var dnsServer = CurrentDNSServer;
var ni = CurrentInterface;
if (dnsServer == null)
{
return;
}
if (ni == null)
{
return;
}
var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
var moc = wmi.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
//如果没有启用IP设置的网络设备则跳过
if (ni.Id.Equals(mo["SettingId"]) == false)
{
continue;
}
var inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = new[]
{
dnsServer.Server1, dnsServer.Server2
};
mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
break;
}
wmi.Dispose();
MessageBox.Show("设置DNS成功");
LoadNI();
}
示例10: GetDefaultGateWay
/// <summary>
/// ���ر���Ĭ������
/// </summary>
/// <returns></returns>
public static IPAddress GetDefaultGateWay()
{
string ip = "";
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
foreach (ManagementObject nic in nics)
{
if (Convert.ToBoolean(nic["ipEnabled"]) == true)
{
//Console.WriteLine((nic["IPAddress"] as String[])[0]);
//Console.WriteLine((nic["IPSubnet"] as String[])[0]);
ip = (nic["DefaultIPGateway"] as String[])[0];
}
}
nics.Dispose();
mc.Dispose();
}
catch { ip = "0.0.0.0"; }
return IPAddress.Parse(ip);
}
示例11: AddSharePermission
/// <summary>
/// Adds the share permissions.
/// </summary>
/// <param name="accountName">Name of the account.</param>
public void AddSharePermission(string accountName)
{
ManagementObject trustee = null;
ManagementObject ace = null;
ManagementObject win32LogicalSecuritySetting = null;
ManagementObject share = null;
ManagementBaseObject getSecurityDescriptorReturn = null;
ManagementBaseObject securityDescriptor = null;
try
{
//// Not necessary
//// NTAccount ntAccount = new NTAccount(accountName);
//// SecurityIdentifier sid = (SecurityIdentifier)ntAccount.Translate(typeof(SecurityIdentifier));
//// byte[] sidArray = new byte[sid.BinaryLength];
//// sid.GetBinaryForm(sidArray, 0);
trustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null);
trustee["Name"] = accountName;
//// trustee["SID"] = sidArray;
ace = new ManagementClass(new ManagementPath("Win32_Ace"), null);
//// Permissions mask http://msdn.microsoft.com/en-us/library/windows/desktop/aa394186(v=vs.85).aspx
ace["AccessMask"] = 0x1F01FF;
//// ace["AccessMask"] = 0x1FF;
ace["AceFlags"] = 3;
ace["AceType"] = 0;
ace["Trustee"] = trustee;
win32LogicalSecuritySetting = new ManagementObject(@"root\cimv2:Win32_LogicalShareSecuritySetting.Name='" + this.shareName + "'");
getSecurityDescriptorReturn = win32LogicalSecuritySetting.InvokeMethod("GetSecurityDescriptor", null, null);
if ((uint)getSecurityDescriptorReturn["ReturnValue"] != 0)
{
throw new WindowsShareException("Unable to add share permission. Error Code: " + getSecurityDescriptorReturn["ReturnValue"]);
}
securityDescriptor = getSecurityDescriptorReturn["Descriptor"] as ManagementBaseObject;
ManagementBaseObject[] dacl = securityDescriptor["DACL"] as ManagementBaseObject[];
if (dacl == null)
{
dacl = new ManagementBaseObject[] { ace };
}
else
{
Array.Resize(ref dacl, dacl.Length + 1);
dacl[dacl.Length - 1] = ace;
}
securityDescriptor["DACL"] = dacl;
share = new ManagementObject(@"root\cimv2:Win32_Share.Name='" + this.shareName + "'");
uint setShareInfoReturn = (uint)share.InvokeMethod("SetShareInfo", new object[] { null, null, securityDescriptor });
if (setShareInfoReturn != 0)
{
throw new WindowsShareException("Unable to add share permission. Error code: " + setShareInfoReturn.ToString(CultureInfo.CurrentCulture));
}
}
catch (Exception ex)
{
throw new WindowsShareException("Unable to add share permission", ex);
}
finally
{
if (trustee != null)
{
trustee.Dispose();
}
if (ace != null)
{
ace.Dispose();
}
if (win32LogicalSecuritySetting != null)
{
win32LogicalSecuritySetting.Dispose();
}
if (getSecurityDescriptorReturn != null)
{
getSecurityDescriptorReturn.Dispose();
}
if (securityDescriptor != null)
{
securityDescriptor.Dispose();
}
if (share != null)
{
share.Dispose();
}
//.........这里部分代码省略.........
示例12: CPUType
/// <summary>
/// Retrieve the CPU Type (Vendor + Model + Speed)
/// </summary>
/// <returns>A string containing the CPU Type</returns>
private static string CPUType()
{
string retVal="";
ManagementClass cim=new ManagementClass("Win32_Processor");
ManagementObjectCollection moc=cim.GetInstances();
foreach (ManagementObject mo in moc)
{
try
{
retVal=mo.Properties["Name"].Value.ToString();
retVal+=" "+mo.Properties["CurrentClockSpeed"].Value.ToString()+"MHz";
}
catch(NullReferenceException)
{
continue;
}
}
cim.Dispose();
moc.Dispose();
return retVal;
}
示例13: GetMemory
/// <summary>
/// 获取系统物理内存大小
/// </summary>
/// <returns>系统物理内存大小,支持64bit,单位MB</returns>
public static ulong GetMemory()
{
try
{
double capacity = 0.0;
var cimobject1 = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (var o in moc1)
{
var mo1 = (ManagementObject)o;
capacity += ((Math.Round(long.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024.0, 1)));
}
moc1.Dispose();
cimobject1.Dispose();
ulong qmem = Convert.ToUInt64(capacity.ToString(CultureInfo.InvariantCulture));
return qmem;
}
catch (System.Runtime.InteropServices.COMException ex)
{
Logger.error("Failed to get the physical memory");
Logger.error(ex);
return ulong.MaxValue;
}
}
示例14: loadconfig
private void loadconfig()
{
#region 旧的配置文件更新
if (File.Exists("2dmmclauncher.cfg"))
{
StreamReader ocfg = new StreamReader("2dmmclauncher.cfg");
string oname = ocfg.ReadLine();
string ojavaxmx = ocfg.ReadLine();
string ojavaw = ocfg.ReadLine();
XmlDocument cfg = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = cfg.CreateXmlDeclaration("1.0", "utf-8", null);
cfg.AppendChild(xmldecl);
XmlElement cfgvalue = cfg.CreateElement("edmmc");
cfg.AppendChild(cfgvalue);
XmlNode cfgroot = cfg.SelectSingleNode("edmmc");
XmlElement player = cfg.CreateElement("PlayerInfo");
player.SetAttribute("playername", oname);
cfgvalue.AppendChild(player);
XmlElement JavaInfo = cfg.CreateElement("JavaInfo");
JavaInfo.SetAttribute("javaxmx", ojavaxmx);
JavaInfo.SetAttribute("javaw", ojavaw);
cfgvalue.AppendChild(JavaInfo);
cfg.AppendChild(cfgvalue);
cfg.Save(cfgfile);
ocfg.Close();
File.Delete("2dmmclauncher.cfg");
}
#endregion
if (File.Exists(cfgfile))
{
XmlDocument cfg = new XmlDocument();
cfg.Load(cfgfile);
XmlNode cfgroot = cfg.SelectSingleNode("edmmc");
XmlElement playerinfo = (XmlElement )cfgroot.SelectSingleNode("PlayerInfo");
playername = playerinfo.Attributes["playername"].Value;
XmlElement javainfo = (XmlElement)cfgroot.SelectSingleNode("JavaInfo");
javaw = javainfo.Attributes["javaw"].Value;
javaxmx=javainfo.Attributes["javaxmx"].Value;
}
else
{
playername = Interaction.InputBox("请输入用户名(仅影响单机模式,用于单机模式获取皮肤)", "用户名", "Player");
XmlDocument cfg=new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = cfg.CreateXmlDeclaration("1.0", "utf-8",null);
cfg.AppendChild(xmldecl);
XmlElement cfgvalue = cfg.CreateElement("edmmc");
cfg.AppendChild(cfgvalue);
XmlNode cfgroot = cfg.SelectSingleNode("edmmc");
XmlElement player = cfg.CreateElement("PlayerInfo");
player.SetAttribute("playername", playername);
cfgvalue.AppendChild(player);
double capacity = 0.0;
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo1 in moc1)
{
capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024.0, 1)));
}
moc1.Dispose();
cimobject1.Dispose();
int qmem = Convert.ToUInt16(capacity.ToString()) / 4;
if (qmem < 512)
{
qmem = 512;
}
javaxmx = qmem.ToString ();
XmlElement JavaInfo = cfg.CreateElement("JavaInfo");
JavaInfo.SetAttribute("javaxmx", javaxmx);
{
try
{
RegistryKey lm = Registry.LocalMachine;
RegistryKey sf = lm.OpenSubKey("SOFTWARE");
RegistryKey js = sf.OpenSubKey("JavaSoft");
RegistryKey jre = js.OpenSubKey("Java Runtime Environment");
RegistryKey reg = Registry.LocalMachine;
reg = reg.OpenSubKey("SOFTWARE").OpenSubKey("JavaSoft").OpenSubKey("Java Runtime Environment");
foreach (string ver in jre.GetSubKeyNames())
{
RegistryKey command = jre.OpenSubKey(ver);
string str = command.GetValue("JavaHome").ToString();
if (str != "")
{
javaw = str + @"\bin\javaw.exe";
break;
}
}
}
catch
{
MessageBox.Show("获取javaw.exe目录失败,请手动查找");
OpenFileDialog javawp = new OpenFileDialog();
javawp.Multiselect = false;
javawp.Title = "请选择javaw.exe";
javawp.Filter = "javaw.exe|javaw.exe";
//.........这里部分代码省略.........
示例15: CreateRestorePoint
private void CreateRestorePoint()
{
ManagementClass WMIClass = null;
try
{
ManagementScope scope = new ManagementScope(ComputerWMIHelper.WMI_Path_Default);
scope.Connect();
WMIClass = new ManagementClass(ComputerWMIHelper.WMI_Class_SystemRestore);
WMIClass.Scope = scope;
//create restore point
ManagementBaseObject inParams = WMIClass.GetMethodParameters("CreateRestorePoint");
object[] param = { Description, intRestorePoint, 100 }; // the event type will be always 100,Begin_System_Change
ret = Convert.ToInt32(WMIClass.InvokeMethod("CreateRestorePoint", param), System.Globalization.CultureInfo.CurrentCulture);
}
catch (Exception ex)
{
// We catch all exceptions because we don't want the exception to be thrown from a separate worker thread
exceptionfromnewthread = ex;
}
finally
{
if (WMIClass != null)
{
WMIClass.Dispose();
}
}
}