首页 > 代码库 > 跨服务器存放文件(1)

跨服务器存放文件(1)

 

 1 public class DataExtractor 2     { 3         static DMSLogger logger = new DMSLogger("C:\\AcctrueSaveXmlToRemoteLogs", "PDM"); 4         /// <summary> 5         /// 将xml文件保存到文件服务器 6         /// </summary> 7         /// <param name="RemoteDirTo">共享目录</param> 8         /// <param name="RemoteDirTo">共享目录子目录</param> 9         /// <param name="RemoteDirToUser">共享目录服务器用户</param>10         /// <param name="RemoteDirToPwd">共享目录服务器密码</param>11         /// <param name="RemoteDirFrom">本地文件</param>12         /// <param name="RemoteDirFromUser">本地用户</param>13         /// <param name="RemoteDirFromPwd">本地密码</param>14         public static void SaveXmlToFileService(string RemoteDirTo, string FilePathTo, string RemoteDirToUser, string RemoteDirToPwd, string file, string RemoteDirFrom, string RemoteDirFromUser, string RemoteDirFromPwd)15         {16             try17             {18                 Reconnect(RemoteDirFrom, RemoteDirFromUser, RemoteDirFromPwd);19                 Reconnect(RemoteDirTo, RemoteDirToUser, RemoteDirToPwd);20                 if (!Directory.Exists(FilePathTo))21                 {22                     try23                     {24                         Directory.CreateDirectory(FilePathTo);25                         logger.WriteDebug("创建目录[" + FilePathTo + "]");26                     }27                     catch (Exception ex)28                     {29                         throw new Exception("[创建目录[" + FilePathTo + "]失败:]" + ex.Message);30                     }31                 }32 33                 if (File.Exists(file))34                 {35                     logger.WriteDebug("开始将文件[" + file + "]保存到[" + FilePathTo + "]");36                     File.Copy(file, FilePathTo + "\\" + Path.GetFileName(file), true);37                     logger.WriteDebug("保存文件[" + Path.GetFileName(file) + "]到[" + FilePathTo + "]成功");38                 }39                 else40                 {41                     logger.WriteDebug("文件[" + file + "]不存在");42                 }43                 //File.Delete(file);44             }45             catch (Exception ex)46             {47                 logger.WriteException("保存Xml文件到文件服务器失败", ex);48                 throw new Exception("保存xml文件到文件服务器失败:" + ex.Message);49             }50         }51 52 53         public static void Reconnect(string connectRemotePath, string user, string pwd)54         {55             try56             {57                 if (connectRemotePath.EndsWith("\\"))58                     connectRemotePath = connectRemotePath.Substring(0, connectRemotePath.Length - 1);59                 System.Diagnostics.Process p = new System.Diagnostics.Process();60                 p.StartInfo.FileName = "CMD.exe";61                 p.StartInfo.Arguments = "   /c   net   use   \"" + connectRemotePath + "\"   /user:\"" + user + "\"   " + pwd;62                 p.StartInfo.RedirectStandardOutput = true;63                 p.StartInfo.RedirectStandardInput = true;64                 p.StartInfo.UseShellExecute = false;65                 p.StartInfo.CreateNoWindow = true;66                 p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;67                 //p.StandardInput.Write(@"net   use   " + connectRemotePath + "/user:" + user + "   " + pwd + " /PERSISTENT:YES");68                 //p.StandardInput.WriteLine("exit");69                 p.Start();70                 p.WaitForExit(1000);71                 p.Close();72                 p.Dispose();73                 logger.WriteDebug("服务器用户[" + user + "]连接成功");74             }75             catch (Exception ex)76             {77                 throw new Exception("访问共享目录出错:" + ex.Message);78             }79         }80     }

 

跨服务器存放文件(1)