跨服务器保存文件:代码实现业务是将一个服务器上的文件保存到另一台服务器的共享目录下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 11 namespace 网络共享做本地映射 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 21 string RemoteDirTo = @"\Netnetnet-pc站点发布"; //共享目录 22 string FilePathTo = @"Test";//共享目录(子目录) 23 string RemoteDirToUser = "admin2";//共享目录服务器用户 24 string RemoteDirToPwd = "123";//共享目录服务器密码 25 string RemoteDirFromFile = @"d:aaaa.txt";//本地文件 26 string RemoteDirFromUser = "administrator";//本地用户 27 string RemoteDirFromPwd = "zmkm";//本地密码 28 29 private void button3_Click(object sender, EventArgs e) 30 { 31 try 32 { 33 DataExtractor.SaveFileToService(RemoteDirTo, FilePathTo, RemoteDirToUser, RemoteDirToPwd, RemoteDirFromFile, RemoteDirFromUser, RemoteDirFromPwd); 34 } 35 catch (Exception ex) 36 { 37 MessageBox.Show(ex.Message); 38 } 39 } 40 41 42 } 43 }
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 6 namespace 网络共享做本地映射 7 { 8 public class DataExtractor 9 { 10 /// <summary> 11 /// 将文件保存到文件服务器 12 /// </summary> 13 /// <param name="RemoteDirTo">共享目录</param> 14 /// <param name="FilePathTo">共享目录(子目录)</param> 15 /// <param name="RemoteDirToUser">共享目录服务器用户</param> 16 /// <param name="RemoteDirToPwd">共享目录服务器密码</param> 17 /// <param name="RemoteDirFromFile">本地文件</param> 18 /// <param name="RemoteDirFromUser">本地用户</param> 19 /// <param name="RemoteDirFromPwd">本地密码</param> 20 public static void SaveFileToService(string RemoteDirTo, string FilePathTo, string RemoteDirToUser, string RemoteDirToPwd, string RemoteDirFromFile, string RemoteDirFromUser, string RemoteDirFromPwd) 21 { 22 FilePathTo = RemoteDirTo.TrimEnd('\') + "\" + FilePathTo.TrimEnd('\').TrimStart('\'); 23 try 24 { 25 Reconnect(RemoteDirFromFile, RemoteDirFromUser, RemoteDirFromPwd); 26 Reconnect(RemoteDirTo, RemoteDirToUser, RemoteDirToPwd); 27 if (!Directory.Exists(FilePathTo)) 28 { 29 try 30 { 31 Directory.CreateDirectory(FilePathTo); 32 } 33 catch (Exception ex) 34 { 35 throw new Exception("[创建目录[" + FilePathTo + "]失败:]" + ex.Message); 36 } 37 } 38 39 if (File.Exists(RemoteDirFromFile)) 40 { 41 File.Copy(RemoteDirFromFile, FilePathTo + "\" + Path.GetFileName(RemoteDirFromFile), true); 42 } 43 } 44 catch (Exception ex) 45 { 46 throw new Exception("保存xml文件到文件服务器失败:" + ex.Message); 47 } 48 } 49 50 public static void Reconnect(string connectRemotePath, string user, string pwd) 51 { 52 try 53 { 54 connectRemotePath.TrimEnd('\'); 55 System.Diagnostics.Process p = new System.Diagnostics.Process(); 56 p.StartInfo.FileName = "CMD.exe"; 57 p.StartInfo.Arguments = " /c net use "" + connectRemotePath + "" /user:"" + user + "" " + pwd; 58 p.StartInfo.RedirectStandardOutput = true; 59 p.StartInfo.RedirectStandardInput = true; 60 p.StartInfo.UseShellExecute = false; 61 p.StartInfo.CreateNoWindow = true; 62 p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 63 //p.StandardInput.Write(@"net use " + connectRemotePath + "/user:" + user + " " + pwd + " /PERSISTENT:YES"); 64 //p.StandardInput.WriteLine("exit"); 65 p.Start(); 66 p.WaitForExit(1000); 67 p.Close(); 68 p.Dispose(); 69 } 70 catch (Exception ex) 71 { 72 throw new Exception("访问目录出错:" + ex.Message); 73 } 74 } 75 } 76 }