本文整理汇总了C#中System.Management.ManagementObject.InvokeMethod方法的典型用法代码示例。如果您正苦于以下问题:C# ManagementObject.InvokeMethod方法的具体用法?C# ManagementObject.InvokeMethod怎么用?C# ManagementObject.InvokeMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.ManagementObject
的用法示例。
在下文中一共展示了ManagementObject.InvokeMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CleanDirectory
public bool CleanDirectory(DirectoryInfo dirInfo, string[] folderNames)
{
//get list of directories
DirectoryInfo[] dirs = dirInfo.GetDirectories();
//loop each
foreach (DirectoryInfo dir in dirs)
{
bool istarget = false;
foreach (string fname in folderNames)
{
istarget = (dir.Name == fname);
if (istarget) break;
}
if (istarget)
{
//dir.Attributes = FileAttributes.Normal;
//Directory.Delete(dir.FullName, true);
using (ManagementObject mo = new ManagementObject(String.Format("win32_Directory.Name='{0}'", dir.FullName)))
{
mo.Get();
ManagementBaseObject outParams = mo.InvokeMethod("Delete", null, null);
if (Convert.ToInt32(outParams.Properties["ReturnValue"].Value) != 0) return false;
}
}
else if (!CleanDirectory(dir, folderNames))
return false;
}
return true;
}
示例2: SetAuto
string strNS = "root\\CIMV2"; // WMI 類別 (Class)
#endregion Fields
#region Methods
public void SetAuto()
{
// 建立 ManagementObject 物件 ( Scope , Path , options )
objCls = new ManagementObject(strNS, strCls + ".INDEX=" + strIndex, null);
// InvokeMethod 方法 : 在物件上叫用方法。
objCls.InvokeMethod("EnableDHCP", null); // 設定自動取得 IP
objCls.InvokeMethod("ReleaseDHCPLease", null); // 釋放 IP
objCls.InvokeMethod("RenewDHCPLease", null); // 重新取得 IP
}
示例3: SetServiceDesktopInsteract
/// <summary>
/// 设置服务与桌面交互,在serviceInstaller1的AfterInstall事件中使用
/// </summary>
/// <param name="serviceName">服务名称</param>
private void SetServiceDesktopInsteract(string serviceName)
{
ManagementObject wmiService = new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
ManagementBaseObject changeMethod = wmiService.GetMethodParameters("Change");
changeMethod["DesktopInteract"] = true;
ManagementBaseObject utParam = wmiService.InvokeMethod("Change", changeMethod,null);
}
示例4: ChangeServiceStartMode
/// <summary>
/// </summary>
/// <param name="objz"></param>
/// <param name="cmdTag"></param>
/// <returns></returns>
public static bool ChangeServiceStartMode(WmiServiceObj objz, string cmdTag)
{
bool bRel;
var sc = new ServiceController(objz.Name);
string startupType = "";
var myPath = new ManagementPath
{
Server = Environment.MachineName,
NamespacePath = @"root\CIMV2",
RelativePath = string.Format("Win32_Service.Name='{0}'", sc.ServiceName)
};
switch (cmdTag)
{
case "41":
startupType = "Manual";
break;
case "42":
startupType = "Auto";
break;
case "43":
startupType = "Disabled";
break;
}
using (var service = new ManagementObject(myPath))
{
ManagementBaseObject inputArgs = service.GetMethodParameters("ChangeStartMode");
inputArgs["startmode"] = startupType;
service.InvokeMethod("ChangeStartMode", inputArgs, null);
bRel = true;
}
sc.Refresh();
return bRel;
}
示例5: Install
public override void Install(System.Collections.IDictionary savedState)
{
try
{
base.Install(savedState);
// Turn on "Allow service to interact with desktop".
// (Note: A published technique to do this by setting a bit in the service's Type registry value (0x100) does not turn this on, so do as follows.)
// This will let the NotifyIcon appear in the sys tray (with balloon at start-up), but not its context menu.
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope(@"root\CIMv2", options); // Common Information Model, version 2.
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + this.serviceInstaller.ServiceName + "'");
ManagementBaseObject inParameters = wmiService.GetMethodParameters("Change");
inParameters["DesktopInteract"] = true;
ManagementBaseObject outParameters = wmiService.InvokeMethod("Change", inParameters, null);
}
catch (Exception e)
{
string licenseKey = "";
string licenseName = "";
try
{
licenseKey = Properties.Settings.Default.LicenseKey;
licenseName = Properties.Settings.Default.LicenseName;
}
catch { }
Service.Services.LoggingService.AddLogEntry(WOSI.CallButler.ManagementInterface.LogLevel.ErrorsOnly, Utils.ErrorUtils.FormatErrorString(e), true);
CallButler.ExceptionManagement.ErrorCaptureUtils.SendError(e, licenseKey, licenseName, "");
}
}
示例6: DeleteAT
public static string DeleteAT(string srv, string JobID)
{
try
{
string strJobId = "";
ManagementObject mo;
ManagementPath path = ManagementPath.DefaultPath;
path.RelativePath = "Win32_ScheduledJob.JobId=" + "\"" + JobID + "\"";
path.Server = srv;
mo = new ManagementObject(path);
ManagementBaseObject inParams = null;
// use late binding to invoke "Delete" method on "Win32_ScheduledJob" WMI class
ManagementBaseObject outParams = mo.InvokeMethod("Delete", inParams, null);
strJobId = outParams.Properties["ReturnValue"].Value.ToString();
if (strJobId == "0") { return "O JobId ( " + JobID + " ) selecionado foi Apagado!"; }
else { return "Out parameters: ReturnValue= " + strJobId; }
}
catch (UnauthorizedAccessException uex)
{
return "Ocorreu um erro: " + uex.Message;
}
catch (ManagementException mex)
{
return "Ocorreu um erro: " + mex.Message;
}
}
示例7: OnCommitted
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted (savedState);
// Setting the "Allow Interact with Desktop" option for this service.
ConnectionOptions connOpt = new ConnectionOptions();
connOpt.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new ManagementScope(@"root\CIMv2", connOpt);
mgmtScope.Connect();
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + ReflectorMgr.ReflectorServiceName + "'");
ManagementBaseObject inParam = wmiService.GetMethodParameters("Change");
inParam["DesktopInteract"] = true;
ManagementBaseObject outParam = wmiService.InvokeMethod("Change", inParam, null);
#region Start the reflector service immediately
try
{
ServiceController sc = new ServiceController("ConferenceXP Reflector Service");
sc.Start();
}
catch (Exception ex)
{
// Don't except - that would cause a rollback. Instead, just tell the user.
RtlAwareMessageBox.Show(null, string.Format(CultureInfo.CurrentCulture, Strings.ServiceStartFailureText,
ex.ToString()), Strings.ServiceStartFailureTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1, (MessageBoxOptions)0);
}
#endregion
}
示例8: StartService
public static void StartService(string service)
{
string msg = string.Format("starting up service begins: {0}", service);
Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);
string objPath = string.Format("Win32_Service.Name='{0}'", service);
try
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Authentication = AuthenticationLevel.Packet;
connectionOptions.EnablePrivileges = true;
connectionOptions.Username = Singleton<Constants>.UniqueInstance.UserName;
connectionOptions.Password = Singleton<Constants>.UniqueInstance.PassWord;
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", Singleton<Constants>.UniqueInstance.MachineName), connectionOptions);
scope.Connect();
using (ManagementObject serviceObj = new ManagementObject(scope, new ManagementPath(objPath), null))
{
ManagementBaseObject outParams = serviceObj.InvokeMethod("StartService",
null, null);
uint returnValue = (uint)outParams["ReturnValue"];
msg = string.Format("starting up result for service {0} is {1}", service, returnValue);
Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg, LogLevel.Warning);
}
}
catch (Exception ex)
{
msg = string.Format("error starting up service: {0}, {1}", service, ex);
Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg, LogLevel.Warning);
}
msg = string.Format("starting up service ends: {0}", service);
Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);
}
示例9: Win32_SharesSearcher
private static void Win32_SharesSearcher()
{
SelectQuery query = new SelectQuery("select * from Win32_Share where Name=\"" + sharename + "\"");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in searcher.Get())
{
foreach (PropertyData prop in mo.Properties)
{
form.textBox1.AppendText(prop.Name + " = " + mo[prop.Name] + Environment.NewLine); }
//form.textBox1.AppendText(string.Format("Win32ShareName: {0} Description {1} Path {2} ", mo.Properties["Name"].Value, mo.Properties["Description"].Value, mo.Properties["Path"].Value) + Environment.NewLine);
}
}
ManagementObject winShareP = new ManagementObject("root\\CIMV2", "Win32_Share.Name=\"" + sharename + "\"", null);
ManagementBaseObject outParams = winShareP.InvokeMethod("GetAccessMask", null, null);
form.textBox1.AppendText(String.Format("access Mask = {0:x}", outParams["ReturnValue"]) + Environment.NewLine);
ManagementBaseObject inParams = winShareP.GetMethodParameters("SetShareInfo");
form.textBox1.AppendText("SetShareInfor in parameters" + Environment.NewLine);
foreach (PropertyData prop in inParams.Properties)
{
form.textBox1.AppendText(String.Format("PROP = {0}, TYPE = {1} ", prop.Name, prop.Type.ToString()) + Environment.NewLine);
}
Object access = inParams.GetPropertyValue("Access");
//Stopped development here because ShareAFolder project exists. The rest of the development is there
//Maybe should copy Sahare a Folder content to here at some point
}
示例10: addComputerToCollection
protected void addComputerToCollection(string resourceID, string collectionID)
{
/* Set collection = SWbemServices.Get ("SMS_Collection.CollectionID=""" & CollID &"""")
Set CollectionRule = SWbemServices.Get("SMS_CollectionRuleDirect").SpawnInstance_()
CollectionRule.ResourceClassName = "SMS_R_System"
CollectionRule.RuleName = "Static-"&ResourceID
CollectionRule.ResourceID = ResourceID
collection.AddMembershipRule CollectionRule*/
//o.Properties["ResourceID"].Value.ToString();
var pathString = "\\\\srv-cm12-p01.srv.aau.dk\\ROOT\\SMS\\site_AA1" + ":SMS_Collection.CollectionID=\"" + collectionID + "\"";
ManagementPath path = new ManagementPath(pathString);
ManagementObject obj = new ManagementObject(path);
ManagementClass ruleClass = new ManagementClass("\\\\srv-cm12-p01.srv.aau.dk\\ROOT\\SMS\\site_AA1" + ":SMS_CollectionRuleDirect");
ManagementObject rule = ruleClass.CreateInstance();
rule["RuleName"] = "Static-"+ resourceID;
rule["ResourceClassName"] = "SMS_R_System";
rule["ResourceID"] = resourceID;
obj.InvokeMethod("AddMembershipRule", new object[] { rule });
}
示例11: DecompressFolder
private static uint DecompressFolder(string path)
{
using (ManagementObject obj2 = new ManagementObject("Win32_Directory.Name=\"" + path.Replace(@"\", @"\\") + "\""))
{
return (uint)obj2.InvokeMethod("UnCompress", null, null).Properties["ReturnValue"].Value;
}
}
示例12: SetHostname
public string SetHostname(string hostname)
{
var oldName = Environment.MachineName;
_logger.Log("Old host name: " + oldName);
_logger.Log("New host name: " + hostname);
if (string.IsNullOrEmpty(hostname) || oldName.Equals(hostname, StringComparison.InvariantCultureIgnoreCase))
return 0.ToString();
using (var cs = new ManagementObject(@"Win32_Computersystem.Name='" + oldName + "'"))
{
cs.Get();
var inParams = cs.GetMethodParameters("Rename");
inParams.SetPropertyValue("Name", hostname);
var methodOptions = new InvokeMethodOptions(null, TimeSpan.MaxValue);
var outParams = cs.InvokeMethod("Rename", inParams, methodOptions);
if (outParams == null)
return 1.ToString();
var renameResult = Convert.ToString(outParams.Properties["ReturnValue"].Value);
//Restart in 10 secs because we want finish current execution and write response back to Xenstore.
if ("0".Equals(renameResult))
Process.Start(@"shutdown.exe", @"/r /t 10 /f /d p:2:4");
return renameResult;
}
}
示例13: SetNetCfg
public void SetNetCfg(string strIP, string strSubmask, string strGateway, string strDNS1, string strDNS2)
{
// 建立 ManagementObject 物件 ( Scope , Path , options )
objCls = new ManagementObject(strNS, strCls + ".INDEX=" + strIndex, null);
ManagementBaseObject objInPara; // 宣告管理物件類別的基本類別
objInPara = objCls.GetMethodParameters("EnableStatic");
objInPara["IPAddress"] = new string[] { strIP }; // 設定 "IP" 屬性
objInPara["SubnetMask"] = new string[] { strSubmask }; // 設定 "子網路遮罩" 屬性
objCls.InvokeMethod("EnableStatic", objInPara, null);
objInPara = objCls.GetMethodParameters("SetGateways");
objInPara["DefaultIPGateway"] = new string[] { strGateway }; // 設定 "Gateway" 屬性
objCls.InvokeMethod("SetGateways", objInPara, null);
objInPara = objCls.GetMethodParameters("SetDNSServerSearchOrder");
objInPara["DNSServerSearchOrder"] = new string[] { strDNS1, strDNS2 }; // 設定 "DNS" 屬性
objCls.InvokeMethod("SetDNSServerSearchOrder", objInPara, null);
// GetMethodParameters 方法 : 用來取得 SetDNSServerSearchOrder 參數清單。
// InvokeMethod 方法 : 在物件上叫用方法。
}
示例14: SetServiceDesktopInsteract
private void SetServiceDesktopInsteract(string serviceName) {
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService = new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
ManagementBaseObject changeMethod = wmiService.GetMethodParameters("Change");
changeMethod["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", changeMethod, null);
}
示例15: button2_Click
private void button2_Click(object sender, EventArgs e)
{
string AccountName = textBox1.Text,
newNameForAccount = textBox2.Text;
ManagementObject theInstance = new ManagementObject("root\\CIMv2", "Win32_UserAccount.Domain='" + Environment.MachineName + "',Name='" + AccountName + "'", null);
ManagementBaseObject inputParams = theInstance.GetMethodParameters("Rename");
inputParams.SetPropertyValue("Name", newNameForAccount);
ManagementBaseObject outParams = theInstance.InvokeMethod("Rename", inputParams, null);
button2.Hide();
}