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.

196 lines
7.8KB

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