如何使用c#将文件夹从一台服务器复制到另一台服务器

我需要将许多文件夹从服务器复制到另一台服务器。 服务器位于不同的域中。 是应该使用tcp还是ftp? 我有登录凭据来访问服务器。 是否可以使用类似的东西

string sourceFile = @"ServerIP\C:\Users\Public\public\test.txt"; string destinationFile = @"Localhost\C:\Users\Public\private\test.txt"; // To move a file or folder to a new location: System.IO.File.Copy(sourceFile, destinationFile); 

您可以使用下面的代码段抓住这样做的想法。 您可以使用LogonUser来模拟本地组,而不仅仅是域帐户。

要复制目录/文件夹中的所有内容(文件),显然您可以使用System.IO命名空间内的Directory类来获取所有文件信息。

码:

 using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Runtime.InteropServices; using System.Security.Principal; using System.Security.Permissions; public class Form1 { [DllImport("advapi32.DLL", SetLastError = true)] public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); private void Button1_Click(System.Object sender, System.EventArgs e) { IntPtr admin_token = default(IntPtr); WindowsIdentity wid_current = WindowsIdentity.GetCurrent(); WindowsIdentity wid_admin = null; WindowsImpersonationContext wic = null; try { MessageBox.Show("Copying file..."); if (LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, ref admin_token) != 0) { wid_admin = new WindowsIdentity(admin_token); wic = wid_admin.Impersonate(); System.IO.File.Copy("C:\\right.bmp", "\\\\157.60.113.28\\testnew\\right.bmp", true); MessageBox.Show("Copy succeeded"); } else { MessageBox.Show("Copy Failed"); } } catch (System.Exception se) { int ret = Marshal.GetLastWin32Error(); MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString()); MessageBox.Show(se.Message); } finally { if (wic != null) { wic.Undo(); } } } } 

参考: 使用本地帐户进行模拟

有可能使用类似的东西吗?

你不应该自己发现吗?

对于

我需要将许多文件夹从服务器复制到另一台服务器

要使用FTP,您需要使用System.Net.FtpWebRequestSystem.Net.WebRequestMethods.Ftp

看到:

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx ,

http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.aspx

此外,谷歌上的一个简单搜索提供了许多好方法, https://www.google.co.in/search?q = copy + files + from+one+server+to+another+using+asp.net

请尝试其中一个,当你实施它们时遇到问题,你应该来这里问“那个”问题。 不是相反。