c#通过WMI实现远程⽂件操作using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.IO;
using System.Diagnostics;
using System.Threading;
using Log;
using System.Windows.Forms;
namespace BankSDS
{
/
// <summary>
/// 注册表主键枚举
/// </summary>
public enum RootKey : uint
{
HKEY_CLASSES_ROOT = 0x80000000,
HKEY_CURRENT_USER = 0x80000001,
HKEY_LOCAL_MACHINE = 0x80000002,
HKEY_USERS = 0x80000003,
HKEY_CURRENT_CONFIG = 0x80000005
}
/
// <summary>
/// 端机信息结构
/// </summary>
public struct ServerInfo
{
public string ServerID;
public string ServerSec;
public string ServerIP;
public string ServerUser;
public string ServerPw;
/// <summary>
/
// 远程推送的⽬标⽬录
/// </summary>
public string FolderPath;
/// <summary>
/// 远程共享名?
/// </summary>
public string ShareName;
/// <summary>
/// 远程推送的说明
/// </summary>
public string Description;
/
// <summary>
/// 远程推送的⽂件集合
/// </summary>
public string [] strSource;
/// <summary>
/// 推送成功后,要设置的注册表⾃动启动项名称
/// </summary>
public string strTarget;
/// <summary>
/// 传送类型
/// </summary>
public int transType;
public bool Result;
}
/// <summary>
/// ⽤于WMI连接的⽤户信息结构
/// </summary>
public struct UserInfo
{
public string UserName;
public string UserPW;
public bool IsEnable;
}
/// <summary>
/// WMI基本封装
/// </summary>
public class WmiShareFunction
{
public static CLog Wmilog = new CLog(Application.StartupPath + @"\\AppLogs\\Wmi.log");
/// <summary>
/// 创建远程⽬录
/// </summary>
/// <param name="scope">ManagementScope Instance</param>
/
// <param name="DirectoryName">要建⽴的⽬录名</param>
/// <param name="CurrentDirectory">根⽬录所在位置,缺省为C:\</param>
/// <returns>0表⽰建⽴成功,-1表⽰建⽴失败</returns>
public static int CreateRemoteDirectory(ManagementScope scope, string DirectoryName, string CurrentDirectory)        {
try
{
string currentDirectory = CurrentDirectory;
if (String.IsNullOrEmpty(CurrentDirectory))
{
currentDirectory = @"C:\";
}
ObjectGetOptions objectGetOptions = new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processBatch = new ManagementClass(scope, managementPath, objectGetOptions);                ManagementBaseObject inParams = processBatch.GetMethodParameters("Create");
inParams["CommandLine"] = @"cmd /CMd " + DirectoryName;
inParams["CurrentDirectory"] = currentDirectory;
ManagementBaseObject outParams = null;
outParams = processBatch.InvokeMethod("Create", inParams, null);
param name
Wmilog.WriteToLog("Excute CreateRemoteDirectory:" + outParams.ToString());
}
catch (Exception ex)
{
Wmilog.WriteToLog("in CreateRemoteDirectory Exception:" + ex.Message);
return -1;
}
Wmilog.WriteToLog("建远程⽬录成功!");
return 0;
}
/// <summary>
/// 在远程⽬标机器上创建⼀个注册表主键
/// </summary>
/
// <param name="connectionScope">ManagementScope</param>
/// <param name="machineName">⽬标机器IP</param>
/// <param name="BaseKey">注册表分⽀名</param>
/// <param name="key">主键名称</param>
/// <returns>创建成功则返回0</returns>
public static int CreateRemoteKey(ManagementScope connectionScope,
string machineName,
RootKey BaseKey,
string key)
{
try
{
ObjectGetOptions objectGetOptions = new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
connectionScope.Path = new ManagementPath(@"\\" + machineName + @"\root\DEFAULT:StdRegProv");                connectionScope.Connect();
ManagementClass registryTask = new ManagementClass(connectionScope,
new ManagementPath(@"DEFAULT:StdRegProv"), objectGetOptions);
ManagementBaseObject methodParams = registryTask.GetMethodParameters("CreateKey");
methodParams["hDefKey"] = BaseKey;
methodParams["sSubKeyName"] = key;
ManagementBaseObject exitCode = registryTask.InvokeMethod("CreateKey",
methodParams, null);
Wmilog.WriteToLog("in CreateRemoteKey:" + exitCode.ToString());
}
catch (ManagementException e)
{
Wmilog.WriteToLog("in CreateRemoteKey(ManagementException):" + e.Message);
return -1;
}
Wmilog.WriteToLog("注册表主键创建成功!");
return 0;
}
/// <summary>
/
// 在远程⽬标机器上创建⼀个注册表键值
/// </summary>
/// <param name="connectionScope">ManagementScope</param>
/// <param name="machineName">⽬标机器IP</param>
/// <param name="BaseKey">注册表分⽀名</param>
/// <param name="key">主键名称</param>
/// <param name="valueName">键值名称</param>
/// <param name="value">键值</param>
/// <returns>创建成功则返回0</returns>
public static int CreateRemoteValue(ManagementScope connectionScope,
string machineName,
RootKey BaseKey,
string key,
string valueName,
string value)
{
try
{
ObjectGetOptions objectGetOptions = new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
connectionScope.Path = new ManagementPath(@"\\" + machineName + @"\root\DEFAULT:StdRegProv");                connectionScope.Connect();
ManagementClass registryTask = new ManagementClass(connectionScope,
new ManagementPath(@"DEFAULT:StdRegProv"), objectGetOptions);
ManagementBaseObject methodParams = registryTask.GetMethodParameters("SetStringValue");
methodParams["hDefKey"] = BaseKey;
methodParams["sSubKeyName"] = key;
methodParams["sValue"] = value;
methodParams["sValueName"] = valueName;
ManagementBaseObject exitCode = registryTask.InvokeMethod("SetStringValue",
methodParams, null);
Wmilog.WriteToLog("in CreateRemoteValue:" + exitCode.ToString());
}
catch (ManagementException e)
{
Wmilog.WriteToLog("in CreateRemoteValue(ManagementException):" + e.Message);
return -1;
}
Wmilog.WriteToLog("注册表键值写⼊成功!");
return 0;
}
/// <summary>
/// 执⾏远程⽂件/⽬录推送
/// </summary>
/// <param name="Server">⽬标地址</param>
/
// <param name="Username">⽬标设备登录⽤户</param>
/// <param name="Password">⽬标设备登录PWD</param>
/// <param name="FolderPath">远程⽬标⽬录</param>
/// <param name="ShareName">⽹络共享名</param>
/// <param name="Description">⽹络共享描述</param>
/// <param name="reginfo">推送成功后,可以设置的注册表项名称</param>
/// <param name="strSources">要推送的源⽂件信息</param>
/// <param name="iType">推送类型;0表⽰推送⽂件,1表⽰推送⽬录</param>
/// <returns>推送成功则返回true</returns>
public static bool WmiFileCopyToRemote(string Server,
string Username,
string Password,
string FolderPath,
string ShareName,
string Description,
string reginfo,
string[] strSources,
int iType
)
{
#region 远程共享⽬录组合
//远程共享名
StringBuilder TargetDir = new StringBuilder();
TargetDir.Append(@"\\");
TargetDir.Append(Server);
TargetDir.Append("\\");
TargetDir.Append(ShareName);
TargetDir.Append("\\");
#endregion
#region 注册表值组合
StringBuilder KeyValue = new StringBuilder();
KeyValue.Append(FolderPath);
KeyValue.Append("\\");
#endregion
#region WMI连接设置
ConnectionOptions options = new ConnectionOptions();
options.Username = Username;
options.Password = Password;
//options.Authority = "ntdlmdomain:DOMAIN";
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;
//options.Authentication = AuthenticationLevel.Connect;
//options.Authority
ManagementScope scope = new ManagementScope(
"\\\\" + Server + "\\root\\cimv2",
options);
#endregion
try
{
Wmilog.WriteToLog("开始WMI连接...... ");
scope.Connect();
if (scope.IsConnected)
{
Wmilog.WriteToLog("WMI连接成功......");
Wmilog.WriteToLog("建远程⽬录开始......");
CreateRemoteDirectory(scope, FolderPath, "");
Wmilog.WriteToLog("建远程⽬录结束......");
Wmilog.WriteToLog("建远程共享⽬录开始......");
CreateShareNetFolder(scope, FolderPath, ShareName, Description);
Wmilog.WriteToLog("建远程共享⽬录结束......");
Wmilog.WriteToLog("连接远程共享开始......");
CreateShareNetConnect(Server, ShareName, Username, Password);
Wmilog.WriteToLog("连接远程共享结束......");
//通过远程共享名,向远程共享⽬录中拷贝⽂件
if (iType == 0)
{
/
/⽂件拷贝
Wmilog.WriteToLog("in CopyFiles:" + "Start".ToString());
System.Diagnostics.Debug.WriteLine("in CopyFiles:" + "Start".ToString());
foreach (string filename in strSources)
{
if (!String.IsNullOrEmpty(filename))
{
FileInfo fileinfo = new FileInfo(filename);
File.Copy(filename, TargetDir + fileinfo.Name, true);
System.Diagnostics.Debug.WriteLine(filename + " " + TargetDir + fileinfo.Name);
}
}
KeyValue.Append(reginfo);
Wmilog.WriteToLog("in CopyFiles:" + "End".ToString());
System.Diagnostics.Debug.WriteLine("in CopyFiles:" + "End".ToString());
}
else if (iType == 1)
{
//⽬录拷贝
DirectoryInfo DirInfo = new DirectoryInfo(strSources[0]);
KeyValue.Append(DirInfo.Name);
KeyValue.Append("\\");
KeyValue.Append(reginfo);
Wmilog.WriteToLog("in CopyDirectory:" + "Start".ToString());
System.Diagnostics.Debug.WriteLine("in CopyDirectory:" + "Start".ToString());
CopyDirectory(strSources[0], TargetDir + DirInfo.Name);
Wmilog.WriteToLog("in CopyDirectory:" + "End".ToString());
System.Diagnostics.Debug.WriteLine("in CopyDirectory:" + "End".ToString());
}
Wmilog.WriteToLog("断开远程共享开始");
RemoveShareNetConnect(Server, ShareName, Username, Password);
Wmilog.WriteToLog("断开远程共享结束");
Wmilog.WriteToLog("删除远程共享⽬录开始");
RemoveShareNetFolder(scope, ShareName);
Wmilog.WriteToLog("删除远程共享⽬录结束");
if (!String.IsNullOrEmpty(reginfo))
{
Wmilog.WriteToLog("查或建⽴注册表主键开始");
CreateRemoteKey(scope, Server, RootKey.HKEY_LOCAL_MACHINE, @"Software\Microsoft\Windows\CurrentVersion\Run");
Wmilog.WriteToLog("查或建⽴注册表主键结束");
Wmilog.WriteToLog("设置注册表值开始");
CreateRemoteValue(scope, Server, RootKey.HKEY_LOCAL_MACHINE, @"Software\Microsoft\Windows\CurrentVersion\Run", reginfo, KeyValue.ToString());                        Wmilog.WriteToLog("设置注册表值结");
}
}
}
catch (Exception e)
{
Wmilog.WriteToLog("WMI连接失败(Exception): " + e.Message);
System.Diagnostics.Debug.WriteLine("in WmiFileCopyToRemote Exception:" + e.Message);
return false;
}
Wmilog.WriteToLog("WMI操作成功结束 ");
return true;
}
/// <summary>
/// 创建远程共享⽬录
/// </summary>
/// <param name="scope">ManagementScope</param>
/// <param name="FolderPath">要共享的⽬录路径</param>
/// <param name="ShareName">共享名</param>
/// <param name="Description">⽹络共享⽂件夹的描述</param>
/// <returns>0表⽰创建成功,-1表⽰创建失败</returns>
public static int CreateShareNetFolder(ManagementScope scope, string FolderPath, string ShareName, string Description)        {
try
{
//创建⼀个ManagementClass对像
ManagementClass managementClass = new ManagementClass(scope, new ManagementPath("Win32_Share"), null);                //创建ManagementBaseObject的输⼊和输出参数
ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
ManagementBaseObject outParams;
//设置输⼊参数
inParams["Description"] = Description;
inParams["Name"] = ShareName;
inParams["Path"] = FolderPath;
inParams["Type"] = 0x0;//DISK_DRIVE
//其它类型
//DISK_DRIVE=0x0
//PRINT_QUEUE=0x1
//DEVICE = 0x2
//IPC = 0x3
//DISK_DRIVE_ADMIN = 0x80000000
//PRINT_QUEUE_ADMIN=0x80000001
//DEVICE_ADMIN=0x80000002
//IPC_ADMIN=0x80000003
//inParams["MaximunAllowed"] = intMaxConnectionsNum;
//调⽤⽅法ManagementClass对像
outParams = managementClass.InvokeMethod("Create", inParams, null);
Wmilog.WriteToLog("Excute ShareNetFolder Result:" + outParams.ToString());
//检测⽅法是否调⽤成功
if ((uint)(outParams.Properties["RetrunValue"].Value) != 0)
{
throw new Exception("Unable to create net share directiory!");
}
}
catch (Exception ex)
{
Wmilog.WriteToLog("创建远程共享⽬录失败:" + ex.Message);
return -1;
}
Wmilog.WriteToLog("创建远程共享⽬录成功!");
return 0;
}
/// <summary>
/// 移除远程共享⽬录
/// </summary>
/// <param name="scope">ManagementScope</param>
/// <param name="ShareName">共享名</param>
/// <returns>移除成功则返回0</returns>
public static int RemoveShareNetFolder(ManagementScope scope, string ShareName)
{
try
{
//SelectQuery selectQuery = new SelectQuery("Select * from Win32_Share Where Name = '" + ShareName + "'");
ObjectQuery selectQuery = new ObjectQuery("Select * from Win32_Share Where Name = '" + ShareName + "'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, selectQuery);
foreach (ManagementObject mo in searcher.Get())
{
mo.InvokeMethod("Delete", null, null);
}
System.Diagnostics.Debug.WriteLine("in CancelShareNetFolder:" + ShareName);
}
catch (ManagementException me)
{
Wmilog.WriteToLog("移除远程共享⽬录失败(ManagementException):" + me.Message);
return -1;
}
catch (Exception e)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。