1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
namespace WMISample
{
public class CallWMIMethod : System.Windows.Forms.Form
{
private System.Windows.Forms.Label userNameLabel;
private System.Windows.Forms.TextBox userNameBox;
private System.Windows.Forms.TextBox passwordBox;
private System.Windows.Forms.Label passwordLabel;
private System.Windows.Forms.Button OKButton;
private System.Windows.Forms.Button cancelButton;
private System.ComponentModel.Container components = null;
public CallWMIMethod()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.userNameLabel = new System.Windows.Forms.Label();
this.userNameBox = new System.Windows.Forms.TextBox();
this.passwordBox = new System.Windows.Forms.TextBox();
this.passwordLabel = new System.Windows.Forms.Label();
this.OKButton = new System.Windows.Forms.Button();
this.cancelButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// userNameLabel
//
this.userNameLabel.Location = new System.Drawing.Point(16, 8);
this.userNameLabel.Name = "userNameLabel";
this.userNameLabel.Size = new System.Drawing.Size(160, 32);
this.userNameLabel.TabIndex = 0;
this.userNameLabel.Text = "Enter the user name for the remote
computer:";
//
// userNameBox
//
this.userNameBox.Location = new System.Drawing.Point(160,
16);
this.userNameBox.Name = "userNameBox";
this.userNameBox.Size = new System.Drawing.Size(192, 20);
this.userNameBox.TabIndex = 1;
this.userNameBox.Text = "";
//
// passwordBox
//
this.passwordBox.Location = new System.Drawing.Point(160, 48);
this.passwordBox.Name = "passwordBox";
this.passwordBox.PasswordChar = '*';
this.passwordBox.Size = new System.Drawing.Size(192, 20);
this.passwordBox.TabIndex = 3;
this.passwordBox.Text = "";
//
// passwordLabel
//
this.passwordLabel.Location = new System.Drawing.Point(16,
48);
this.passwordLabel.Name = "passwordLabel";
this.passwordLabel.Size = new System.Drawing.Size(160, 32);
this.passwordLabel.TabIndex = 2;
this.passwordLabel.Text = "Enter the password for the remote
computer:";
//
// OKButton
//
this.OKButton.Location = new System.Drawing.Point(40, 88);
this.OKButton.Name = "OKButton";
this.OKButton.Size = new System.Drawing.Size(128, 23);
this.OKButton.TabIndex = 4;
this.OKButton.Text = "OK";
this.OKButton.Click += new System.EventHandler
(this.OKButton_Click);
//
// cancelButton
//
this.cancelButton.DialogResult =
System.Windows.Forms.DialogResult.Cancel;
this.cancelButton.Location = new System.Drawing.Point(200,
88);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Size = new System.Drawing.Size(128, 23);
this.cancelButton.TabIndex = 5;
this.cancelButton.Text = "Cancel";
this.cancelButton.Click += new System.EventHandler
(this.cancelButton_Click);
//
// MyQuerySample
//
this.AcceptButton = this.OKButton;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.cancelButton;
this.ClientSize = new System.Drawing.Size(368, 130);
this.ControlBox = false;
this.Controls.Add(this.cancelButton);
this.Controls.Add(this.OKButton);
this.Controls.Add(this.passwordBox);
this.Controls.Add(this.passwordLabel);
this.Controls.Add(this.userNameBox);
this.Controls.Add(this.userNameLabel);
this.Name = "MyQuerySample";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Remote Connection";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new CallWMIMethod());
}
private void OKButton_Click(object sender, System.EventArgs e)
{
try
{
ConnectionOptions connection = new ConnectionOptions();
connection.Username = userNameBox.Text;
connection.Password = passwordBox.Text;
connection.Authority = "ntlmdomain:***mondomaine***";
ManagementScope scope = new ManagementScope(
"\\\\***monIP***\\root\\CIMV2", connection);
scope.Connect();
ManagementObject classInstance =
new ManagementObject(scope,
new ManagementPath("Win32_Directory.Name='c:\'"),
null); //****destination****
// Obtain in-parameters for the method
ManagementBaseObject inParams =
classInstance.GetMethodParameters("CopyEx");
// Add the input parameters.
inParams["FileName"] = "c:\aaa\test.txt"; //***fichier à
copier***
// Execute the method and obtain the return values.
ManagementBaseObject outParams =
classInstance.InvokeMethod("CopyEx", inParams, null);
// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams
["ReturnValue"]);
Console.WriteLine("StopFileName: " + outParams
["StopFileName"]);
Close();
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to execute
the WMI method: " + err.Message);
}
catch(System.UnauthorizedAccessException unauthorizedErr)
{
MessageBox.Show("Connection error (user name or password
might be incorrect): " + unauthorizedErr.Message);
}
}
private void cancelButton_Click(object sender, System.EventArgs e)
{
Close();
}
}
} |
Partager