码迷,mamicode.com
首页 > 其他好文 > 详细

跨服务器存放文件(1)

时间:2014-09-23 23:59:15      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   文件   div   

 

 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             try
17             {
18                 Reconnect(RemoteDirFrom, RemoteDirFromUser, RemoteDirFromPwd);
19                 Reconnect(RemoteDirTo, RemoteDirToUser, RemoteDirToPwd);
20                 if (!Directory.Exists(FilePathTo))
21                 {
22                     try
23                     {
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                 else
40                 {
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             try
56             {
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)

标签:style   blog   color   io   os   ar   for   文件   div   

原文地址:http://www.cnblogs.com/yf2011/p/3989312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!