CNAS取数仪器端升级
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

171 řádky
6.7KB

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