CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteFileCopy.cs 6.8KB

5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using CnasSynchronousCommon;
  2. using CnasSynchrousModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. namespace CnasSynchronusClient
  10. {
  11. public class RemoteFileCopy
  12. {
  13. public InstrumentDataSourceInfo SyncInstrumentDSInfo { get; set; }
  14. public RemoteFileCopy(InstrumentDataSourceInfo SyncInstrumentDSInfo)
  15. {
  16. this.SyncInstrumentDSInfo = SyncInstrumentDSInfo;
  17. }
  18. public bool isExistFile(string strPath)
  19. {
  20. if (File.Exists(strPath))
  21. return true;
  22. else
  23. return false;
  24. }
  25. public void CopyFileFromRemote(string strPathSuffix,string stAutoFileName=null)
  26. {
  27. if (SyncInstrumentDSInfo.Path == null) return;
  28. string[] strRemoteHostMsgs=SyncInstrumentDSInfo.Path.Split(new char[1] { '\\'}, StringSplitOptions.RemoveEmptyEntries);
  29. if (strRemoteHostMsgs.Length > 0)
  30. {
  31. //执行net user,否则没有权限
  32. if (SyncInstrumentDSInfo.DsPathType == PathType.Remote)
  33. Connect(@"\\"+strRemoteHostMsgs[0], SyncInstrumentDSInfo.RemoteUser, SyncInstrumentDSInfo.RemotePwd);
  34. //执行复制
  35. string strPath = SyncInstrumentDSInfo.Path;
  36. string strCatchPath = FileHelper.getBasePath() + "\\Cache\\";
  37. string strCopyPath = System.Text.RegularExpressions.Regex.Replace(SyncInstrumentDSInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strPathSuffix;
  38. if (stAutoFileName != null)
  39. {
  40. if(Path.GetExtension(strPath)=="")
  41. strPath = strPath + "\\" + stAutoFileName;
  42. strCopyPath = System.Text.RegularExpressions.Regex.Replace(strPath, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strPathSuffix;
  43. if (File.Exists(strCatchPath+strCopyPath))
  44. File.Delete(strCatchPath+strCopyPath);
  45. }
  46. Transport(strPath, strCatchPath, strCopyPath);
  47. }
  48. else
  49. {
  50. AppLog.Error("未能正确获得解析后的远程主机名");
  51. }
  52. }
  53. public static void DeleteDir(string file)
  54. {
  55. try
  56. {
  57. //去除文件夹和子文件的只读属性
  58. //去除文件夹的只读属性
  59. System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
  60. fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
  61. //去除文件的只读属性
  62. System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
  63. //判断文件夹是否还存在
  64. if (Directory.Exists(file))
  65. {
  66. foreach (string f in Directory.GetFileSystemEntries(file))
  67. {
  68. if (File.Exists(f))
  69. {
  70. //如果有子文件删除文件
  71. File.Delete(f);
  72. Console.WriteLine(f);
  73. }
  74. else
  75. {
  76. //循环递归删除子文件夹
  77. DeleteDir(f);
  78. }
  79. }
  80. //删除空文件夹
  81. Directory.Delete(file);
  82. }
  83. }
  84. catch (Exception ex) // 异常处理
  85. {
  86. AppLog.Error(ex.Message);// 异常信息
  87. }
  88. }
  89. public bool Connect(string remoteHostWithShareDirectory, string userName, string passWord)
  90. {
  91. bool Flag = false;
  92. Process proc = new Process();
  93. try
  94. {
  95. proc.StartInfo.FileName = "cmd.exe";
  96. proc.StartInfo.UseShellExecute = false;
  97. proc.StartInfo.RedirectStandardInput = true;
  98. proc.StartInfo.RedirectStandardOutput = true;
  99. proc.StartInfo.RedirectStandardError = true;
  100. proc.StartInfo.CreateNoWindow = true;
  101. proc.Start();
  102. string dosLine = "net use " + remoteHostWithShareDirectory + " " + passWord + " /User:" + userName;
  103. proc.StandardInput.WriteLine(dosLine);
  104. proc.StandardInput.WriteLine("exit");
  105. while (!proc.HasExited)
  106. {
  107. proc.WaitForExit(1000);
  108. }
  109. string errormsg = proc.StandardError.ReadToEnd();
  110. proc.StandardError.Close();
  111. if (String.IsNullOrEmpty(errormsg))
  112. {
  113. Flag = true;
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. AppLog.Error(ex.Message);
  119. throw ex;
  120. }
  121. finally
  122. {
  123. proc.Close();
  124. proc.Dispose();
  125. }
  126. return Flag;
  127. }
  128. /// <summary>
  129.         /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
  130.         /// </summary>
  131.         /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param>
  132.         /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
  133.         /// <param name="fileName">保存文件的名称以及扩展名</param>
  134.         public static void Transport(string src, string dst, string fileName)
  135. {
  136. try
  137. {
  138. FileStream inFileStream = new FileStream(src, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  139. if (!Directory.Exists(dst))
  140. {
  141. Directory.CreateDirectory(dst);
  142. }
  143. dst = dst + fileName;
  144. FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
  145. byte[] buf = new byte[inFileStream.Length];
  146. int byteCount;
  147. while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
  148. {
  149. outFileStream.Write(buf, 0, byteCount);
  150. }
  151. inFileStream.Flush();
  152. inFileStream.Close();
  153. outFileStream.Flush();
  154. outFileStream.Close();
  155. }
  156. catch (Exception ex)
  157. {
  158. AppLog.Error(ex.Message);
  159. }
  160. }
  161. }
  162. }