xbrower关了scp远程拷贝贝还会不会继续?

C#通过WMI实现了远程创建共享目录以及远程拷贝文件等操作
public&static&bool&connectState(string&path)
&&&&&&&&&&&&return&connectState(path,&"",&"");
&&&&&&&&public&static&bool&connectState(string&path,&string&userName,&string&passWord)
&&&&&&&&&&&&bool&Flag&=&false;
&&&&&&&&&&&&Process&proc&=&new&Process();
&&&&&&&&&&&&try
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&proc.StartInfo.FileName&=&"cmd.exe";
&&&&&&&&&&&&&&&&proc.StartInfo.UseShellExecute&=&false;
&&&&&&&&&&&&&&&&proc.StartInfo.RedirectStandardInput&=&true;
&&&&&&&&&&&&&&&&proc.StartInfo.RedirectStandardOutput&=&true;
&&&&&&&&&&&&&&&&proc.StartInfo.RedirectStandardError&=&true;
&&&&&&&&&&&&&&&&proc.StartInfo.CreateNoWindow&=&true;
&&&&&&&&&&&&&&&&proc.Start();
&&&&&&&&&&&&&&&&string&dosLine&=&@"net&use&"&+&path&+&"&/User:"&+&userName&+&"&"&+&passWord&+&"&/PERSISTENT:YES";
&&&&&&&&&&&&&&&&proc.StandardInput.WriteLine(dosLine);
&&&&&&&&&&&&&&&&proc.StandardInput.WriteLine("exit");
&&&&&&&&&&&&&&&&while&(!proc.HasExited)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&proc.WaitForExit(1000);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&string&errormsg&=&proc.StandardError.ReadToEnd();
&&&&&&&&&&&&&&&&proc.StandardError.Close();
&&&&&&&&&&&&&&&&if&(string.IsNullOrEmpty(errormsg))
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&Flag&=&true;
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&else
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&throw&new&Exception(errormsg);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}
&&&&&&&&&&&&catch&(Exception&ex)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&throw&ex;
&&&&&&&&&&&&}
&&&&&&&&&&&&finally
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&proc.Close();
&&&&&&&&&&&&&&&&proc.Dispose();
&&&&&&&&&&&&}
&&&&&&&&&&&&return&Flag;
public&static&void&CopyDir(string&srcPath,&string&aimPath)
&&&&&&&&&&&&try
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&//&检查目标目录是否以目录分割字符结束如果不是则添加之
&&&&&&&&&&&&&&&&if&(aimPath[aimPath.Length&-&1]&!=&Path.DirectorySeparatorChar)
&&&&&&&&&&&&&&&&&&&&aimPath&+=&Path.DirectorySeparatorChar;
&&&&&&&&&&&&&&&&//&判断目标目录是否存在如果不存在则新建之
&&&&&&&&&&&&&&&&if&(!Directory.Exists(aimPath))
&&&&&&&&&&&&&&&&&&&&Directory.CreateDirectory(aimPath);
&&&&&&&&&&&&&&&&//&得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
&&&&&&&&&&&&&&&&//如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
&&&&&&&&&&&&&&&&//string[]&fileList&=&Directory.GetFiles(srcPath);
&&&&&&&&&&&&&&&&string[]&fileList&=&Directory.GetFileSystemEntries(srcPath);
&&&&&&&&&&&&&&&&//遍历所有的文件和目录
&&&&&&&&&&&&&&&&foreach&(string&file&in&fileList)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&//先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
&&&&&&&&&&&&&&&&&&&&if&(Directory.Exists(file))
&&&&&&&&&&&&&&&&&&&&&&&&CopyDir(file,&aimPath&+&Path.GetFileName(file));
&&&&&&&&&&&&&&&&&&&&//否则直接Copy文件
&&&&&&&&&&&&&&&&&&&&else
&&&&&&&&&&&&&&&&&&&&&&&&File.Copy(file,&aimPath&+&Path.GetFileName(file),&true);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}
&&&&&&&&&&&&catch&(Exception&ee)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&throw&new&Exception(ee.ToString());
&&&&&&&&&&&&}
C#通过WMI实现了远程创建共享目录以及远程拷贝文件实现
using System.Collections.G
using System.T
using System.M
using System.IO;
using System.D
using System.T
using System.Windows.F
namespace BankSDS
&&& /// 注册表主键枚举
&&& public enum RootKey : uint
&&&&&&& HKEY_CLASSES_ROOT = 0x,
&&&&&&& HKEY_CURRENT_USER = 0x,
&&&&&&& HKEY_LOCAL_MACHINE = 0x,
&&&&&&& HKEY_USERS = 0x,
&&&&&&& HKEY_CURRENT_CONFIG = 0x
&&& /// 端机信息结构
&&& public struct ServerInfo
&&&&&&& public string ServerID;
&&&&&&& public string ServerS
&&&&&&& public string ServerIP;
&&&&&&& public string ServerU
&&&&&&& public string ServerPw;
&&&&&&& ///
&&&&&&& /// 远程推送的目标目录
&&&&&&& ///
&&&&&&& public string FolderP
&&&&&&& ///
&&&&&&& /// 远程共享名?
&&&&&&& ///
&&&&&&& public string ShareN
&&&&&&& ///
&&&&&&& /// 远程推送的说明
&&&&&&& ///
&&&&&&& public string D
&&&&&&& ///
&&&&&&& /// 远程推送的文件集合
&&&&&&& ///
&&&&&&& public string [] strS
&&&&&&& ///
&&&&&&& /// 推送成功后,要设置的注册表自动启动项名称
&&&&&&& ///
&&&&&&& public string strT
&&&&&&& ///
&&&&&&& /// 传送类型
&&&&&&& ///
&&&&&&& public int transT
&&&&&&& public bool R
&&& /// 用于WMI连接的用户信息结构
&&& public struct UserInfo
&&&&&&& public string UserN
&&&&&&& public string UserPW;
&&&&&&& public bool IsE
&&& /// WMI基本封装
&&& public class WmiShareFunction
&&&&&&& public static CLog Wmilog = new CLog(Application.StartupPath + @"");
&&&&&&& ///
&&&&&&& /// 创建远程目录
&&&&&&& ///
&&&&&&& ///
&&&&&&&&&&&&&&& ManagementClass processBatch = new ManagementClass(scope, managementPath, objectGetOptions);
&&&&&&&&&&&&&&& ManagementBaseObject inParams = processBatch.GetMethodParameters("Create");
&&&&&&&&&&&&&&& inParams["CommandLine"] = @"cmd /CMd " + DirectoryN
&&&&&&&&&&&&&&& inParams["CurrentDirectory"] = currentD
&&&&&&&&&&&&&&& ManagementBaseObject outParams =
&&&&&&&&&&&&&&& outParams = processBatch.InvokeMethod("Create", inParams, null);
&&&&&&&&&&&&&&& Wmilog.WriteToLog("Excute CreateRemoteDirectory:" + outParams.ToString());
&&&&&&&&&&& }
&&&&&&&&&&& catch (Exception ex)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& Wmilog.WriteToLog("in CreateRemoteDirectory Exception:" + ex.Message);
&&&&&&&&&&&&&&& return -1;
&&&&&&&&&&& }
&&&&&&&&&&& Wmilog.WriteToLog("建远程目录成功!");
&&&&&&&&&&& return 0;
&&&&&&& ///
&&&&&&& /// 在远程目标机器上创建一个注册表主键
&&&&&&& ///
&&&&&&& ///
&&&&&&&&&&&&&&& methodParams["hDefKey"] = BaseK
&&&&&&&&&&&&&&& methodParams["sSubKeyName"] =
&&&&&&&&&&&&&&& 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;
&&&&&&& ///
&&&&&&& /// 在远程目标机器上创建一个注册表键值
&&&&&&& ///
&&&&&&& ///
&&&&&&&&&&&&&&& methodParams["hDefKey"] = BaseK
&&&&&&&&&&&&&&& methodParams["sSubKeyName"] =
&&&&&&&&&&&&&&& methodParams["sValue"] =
&&&&&&&&&&&&&&& methodParams["sValueName"] = valueN
&&&&&&&&&&&&&&& 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;
&&&&&&& ///
&&&&&&& /// 执行远程文件/目录推送
&&&&&&& ///
&&&&&&& ///
&&&&&&&&&&& //远程共享名
&&&&&&&&&&& 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 = U
&&&&&&&&&&& options.Password = P
&&&&&&&&&&& //options.Authority = "ntdlmdomain:DOMAIN";
&&&&&&&&&&& options.Impersonation = ImpersonationLevel.I
&&&&&&&&&&& options.EnablePrivileges =
&&&&&&&&&&& //options.Authentication = AuthenticationLevel.C
&&&&&&&&&&& //options.Authority
&&&&&&&&&&& ManagementScope scope = new ManagementScope(
&&&&&&&&&&&&&&&&&&& "\\\\" + Server + "",
&&&&&&&&&&&&&&&&&&& 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);
&&&&&&&&&&&&&&&
&&&&&&&&&&& }
&&&&&&&&&&& Wmilog.WriteToLog("WMI操作成功结束 ");
&&&&&&&&&&&
&&&&&&& ///
&&&&&&& /// 创建远程共享目录
&&&&&&& ///
&&&&&&& ///
&&&&&&&&&&&&&&& //创建ManagementBaseObject的输入和输出参数
&&&&&&&&&&&&&&& ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
&&&&&&&&&&&&&&& ManagementBaseObject outP
&&&&&&&&&&&&&&& //设置输入参数
&&&&&&&&&&&&&&& inParams["Description"] = D
&&&&&&&&&&&&&&& inParams["Name"] = ShareN
&&&&&&&&&&&&&&& inParams["Path"] = FolderP
&&&&&&&&&&&&&&& inParams["Type"] = 0x0;//DISK_DRIVE
&&&&&&&&&&&&&&& //其它类型
&&&&&&&&&&&&&&& //DISK_DRIVE=0x0
&&&&&&&&&&&&&&& //PRINT_QUEUE=0x1
&&&&&&&&&&&&&&& //DEVICE = 0x2
&&&&&&&&&&&&&&& //IPC = 0x3
&&&&&&&&&&&&&&& //DISK_DRIVE_ADMIN = 0x
&&&&&&&&&&&&&&& //PRINT_QUEUE_ADMIN=0x
&&&&&&&&&&&&&&& //DEVICE_ADMIN=0x
&&&&&&&&&&&&&&& //IPC_ADMIN=0x
&&&&&&&&&&&&&&& //inParams["MaximunAllowed"] = intMaxConnectionsN
&&&&&&&&&&&&&&& //调用方法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;
&&&&&&& ///
&&&&&&& /// 移除远程共享目录
&&&&&&& ///
&&&&&&& ///
&&&&&&&&&&& Wmilog.WriteToLog("移除远程共享目录成功!");
&&&&&&&&&&& return 0;
&&&&&&& ///
&&&&&&& /// 使用net use命令远程连接目标机器,拷贝文件
&&&&&&& ///
&&&&&&& ///
&&&&&&&&&&&&&&& process.StartInfo.FileName = "net.exe";
&&&&&&&&&&&&&&& process.StartInfo.Arguments = @"use \" + Server + @"" + ShareName + " "" + Password + "" /user:"" + Username + "" ";
&&&&&&&&&&&&&&& process.StartInfo.CreateNoWindow =
&&&&&&&&&&&&&&& process.StartInfo.UseShellExecute =
&&&&&&&&&&&&&&& process.Start();
&&&&&&&&&&&&&&& System.Diagnostics.Debug.WriteLine(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
&&&&&&&&&&&&&&& process.WaitForExit();
&&&&&&&&&&&&&&& System.Diagnostics.Debug.WriteLine("Start Copy File.....");
&&&&&&&&&&&&&&& File.Copy(strSource, strTarget, true);
&&&&&&&&&&&&&&& System.Diagnostics.Debug.WriteLine("End Copy File.....");
&&&&&&&&&&&&&&& process.StartInfo.Arguments = @"use \" + Server + @"" + ShareName + " /delete";
&&&&&&&&&&&&&&& process.Start();
&&&&&&&&&&&&&&& process.Close();
&&&&&&&&&&& }
&&&&&&&&&&& catch (IOException ex)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& System.Diagnostics.Debug.WriteLine("in FileCopy IOException:" + ex.Message);
&&&&&&&&&&&&&&& return -1;
&&&&&&&&&&& }
&&&&&&&&&&& catch (Exception ex)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& System.Diagnostics.Debug.WriteLine("in FileCopy Exception:" + ex.Message);
&&&&&&&&&&&&&&& return -1;
&&&&&&&&&&& }
&&&&&&&&&&& finally
&&&&&&&&&&& {
&&&&&&&&&&&&&&& process.Dispose();
&&&&&&&&&&& }
&&&&&&&&&&& return 0;
&&&&&&& ///
&&&&&&& /// 通过远程共享名,递归拷贝指定目录下的所有信息到目标目录
&&&&&&& ///
&&&&&&& ///
&&&&&&&&&&& if (dirs.Length & 0)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& foreach (System.IO.DirectoryInfo temDirectoryInfo in dirs)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& string sourceDirectoryFullName = temDirectoryInfo.FullN
&&&&&&&&&&&&&&&&&&& string destDirectoryFullName = sourceDirectoryFullName.Replace(sPath, dPath);
&&&&&&&&&&&&&&&&&&& if (!System.IO.Directory.Exists(destDirectoryFullName))
&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&& System.IO.Directory.CreateDirectory(destDirectoryFullName);
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&& CopyFile(temDirectoryInfo, destDirectoryFullName);
&&&&&&&&&&&&&&&&&&& CopyDirectory(sourceDirectoryFullName, destDirectoryFullName);
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&& ///
&&&&&&& /// 通过远程共享名,拷贝指定目录下的所有文件到目标目录
&&&&&&& ///
&&&&&&& ///
&&&&&&& ///
&&&&&&& /// 用net use命令连接到远程共享目录上,创建网络共享连接
&&&&&&& ///
&&&&&&& ///
&&&&&&& ///
&&&&&&& /// 用net use delete命令移除网络共享连接
&&&&&&& ///
&&&&&&& ///
&&&&&&& ///
远程共享名
&&&&&&& ///
远程登录用户
&&&&&&& ///
远程登录密码
&&&&&&& public static void RemoveShareNetConnect(string Server, string ShareName, string Username, string Password)
&&&&&&&&&&& //System.Diagnostics.Process.Start("net.exe", @"use \" + Server + @"" + ShareName + " "" + Password + "" /user:"" + Username + "" ");
&&&&&&&&&&& Process process = new Process();
&&&&&&&&&&& process.StartInfo.FileName = "net.exe";
&&&&&&&&&&& process.StartInfo.Arguments = @"use \" + Server + @"" + ShareName + " /delete";
&&&&&&&&&&& process.StartInfo.CreateNoWindow =
&&&&&&&&&&& process.StartInfo.UseShellExecute =
&&&&&&&&&&& process.Start();
&&&&&&&&&&& process.WaitForExit();
&&&&&&&&&&& process.Close();
&&&&&&&&&&& process.Dispose();
&&&&&&& ///
远程共享名
&&&&&&& ///
远程登录用户
&&&&&&& ///
远程登录密码
&&&&&&& public static void CreateShareNetConnect(string Server, string ShareName, string Username, string Password)
&&&&&&&&&&& Process process = new Process();
&&&&&&&&&&& process.StartInfo.FileName = "net.exe";
&&&&&&&&&&& process.StartInfo.Arguments = @"use \" + Server + @"" + ShareName + " "" + Password + "" /user:"" + Username + "" ";
&&&&&&&&&&& process.StartInfo.CreateNoWindow =
&&&&&&&&&&& process.StartInfo.UseShellExecute =
&&&&&&&&&&& process.Start();
&&&&&&&&&&& process.WaitForExit();
&&&&&&&&&&& process.Close();
&&&&&&&&&&& process.Dispose();
&&&&&&& ///
目的路径()
&&&&&&& public static void CopyFile(System.IO.DirectoryInfo path, string desPath)
&&&&&&&&&&& string sourcePath = path.FullN
&&&&&&&&&&& System.IO.FileInfo[] files = path.GetFiles();
&&&&&&&&&&& foreach (System.IO.FileInfo file in files)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& string sourceFileFullName = file.FullN
&&&&&&&&&&&&&&& string destFileFullName = sourceFileFullName.Replace(sourcePath, desPath);
&&&&&&&&&&&&&&& file.CopyTo(destFileFullName, true);
&&&&&&&&&&& }
&&&&&&& ///
&&&&&&& public static void CopyDirectory(string sPath, string dPath)
&&&&&&&&&&& string[] directories = System.IO.Directory.GetDirectories(sPath);
&&&&&&&&&&& if (!System.IO.Directory.Exists(dPath))
&&&&&&&&&&&&&&& System.IO.Directory.CreateDirectory(dPath);
&&&&&&&&&&& System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(sPath);
&&&&&&&&&&& System.IO.DirectoryInfo[] dirs = dir.GetDirectories();
&&&&&&&&&&& CopyFile(dir, dPath);
&&&&&&& ///
&&&&&&& ///
&&&&&&& ///
远程共享名
&&&&&&& ///
远程登录用户
&&&&&&& ///
远程登录密码
&&&&&&& /// 拷贝成功则返回0
&&&&&&& public static int FileCopy(string strSource, string strTarget, string Server, string ShareName, string Username, string Password)
&&&&&&&&&&& Process process = new Process();
&&&&&&&&&&& try
&&&&&&&&&&& {
ManagementScope
&&&&&&& ///
&&&&&&& /// 移除成功则返回0
&&&&&&& 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)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& Wmilog.WriteToLog("移除远程共享目录失败(Exception):" + e.Message);
&&&&&&&&&&&&&&& return -1;
&&&&&&&&&&& }
ManagementScope
&&&&&&& ///
要共享的目录路径
&&&&&&& ///
&&&&&&& ///
网络共享文件夹的描述
&&&&&&& /// 0表示创建成功,-1表示创建失败
&&&&&&& public static int CreateShareNetFolder(ManagementScope scope, string FolderPath, string ShareName, string Description)
&&&&&&&&&&& try
&&&&&&&&&&& {
&&&&&&&&&&&&&&& //创建一个ManagementClass对像
&&&&&&&&&&&&&&& ManagementClass managementClass = new ManagementClass(scope, new ManagementPath("Win32_Share"), null);
&&&&&&& ///
目标设备登录用户
&&&&&&& ///
目标设备登录PWD
&&&&&&& ///
远程目标目录
&&&&&&& ///
网络共享名
&&&&&&& ///
网络共享描述
&&&&&&& ///
推送成功后,可以设置的注册表项名称
&&&&&&& ///
要推送的源文件信息
&&&&&&& ///
推送类型;0表示推送文件,1表示推送目录
&&&&&&& /// 推送成功则返回true
&&&&&&& public static bool WmiFileCopyToRemote(string Server,
&&&&&&&&&&&&&&&&&&&&& string Username,
&&&&&&&&&&&&&&&&&&&&& string Password,
&&&&&&&&&&&&&&&&&&&&& string FolderPath,
&&&&&&&&&&&&&&&&&&&&& string ShareName,
&&&&&&&&&&&&&&&&&&&&& string Description,
&&&&&&&&&&&&&&&&&&&&& string reginfo,
&&&&&&&&&&&&&&&&&&&&& string[] strSources,
&&&&&&&&&&&&&&&&&&&&& int iType
&&&&&&&&&&&&&&&&&&&& )
&&&&&&&&&&& #region 远程共享目录组合
ManagementScope
&&&&&&& ///
目标机器IP
&&&&&&& ///
注册表分支名
&&&&&&& ///
&&&&&&& ///
&&&&&&& ///
&&&&&&& /// 创建成功则返回0
&&&&&&& 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");
ManagementScope
&&&&&&& ///
目标机器IP
&&&&&&& ///
注册表分支名
&&&&&&& ///
&&&&&&& /// 创建成功则返回0
&&&&&&& 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");
ManagementScope Instance
&&&&&&& ///
要建立的目录名
&&&&&&& ///
根目录所在位置,缺省为C:\
&&&&&&& /// 0表示建立成功,-1表示建立失败
&&&&&&& public static int CreateRemoteDirectory(ManagementScope scope, string DirectoryName, string CurrentDirectory)
&&&&&&&&&&& try
&&&&&&&&&&& {
&&&&&&&&&&&&&&& string currentDirectory = CurrentD
&&&&&&&&&&&&&&& if (String.IsNullOrEmpty(CurrentDirectory))
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& currentDirectory = @"C:";
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& ObjectGetOptions objectGetOptions = new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
&&&&&&&&&&&&&&& ManagementPath managementPath = new ManagementPath("Win32_Process");
CopyDir(目标,本地路径);
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 远程拷贝 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信