CNAS取数仪器端升级
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

182 líneas
7.6KB

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