简体   繁体   English

将文件从服务器复制到另一个

[英]copy file from server to another

I have to copy some file from server to another, I thought to use File.Copy but how I specify there the location of a file ?我必须将一些文件从服务器复制到另一个,我想使用File.Copy但是我如何在那里指定文件的位置? and also there can be a password identification on from and to.并且还可以在 from 和 to 上设置密码标识。

Any ideas ?有任何想法吗 ?

If these are two servers on the same local network and you have appropriate access permissions, you can probably get by using the File.Copy method, and passing whatever path you would normally use to access the network share in Windows Explorer (this is probably a UNC path, like \\\\TheRemoteServer\\SharedFolder\\MyFiles ).如果这是同一本地网络上的两台服务器,并且您有适当的访问权限,您可能可以通过使用File.Copy方法,并传递通常用于访问 Windows 资源管理器中的网络共享的任何路径(这可能是一个UNC 路径,如\\\\TheRemoteServer\\SharedFolder\\MyFiles )。 If you can copy the file in Explorer, the File.Copy method will more than likely succeed also.如果您可以在资源管理器中复制文件,那么File.Copy方法也很可能会成功。

However, if you need to authenticate as a different user on the remote server to have the appropriate permissions to copy files, things get a little more complicated because there is no easy .NET API for this.但是,如果您需要在远程服务器上以不同的用户身份进行身份验证以获得适当的权限来复制文件,事情会变得有点复杂,因为没有简单的 .NET API 来实现这一点。 You might look into this answer to another question .您可能会查看另一个问题的答案 Essentially, it p/invokes to log on to the remote machine as a different user.本质上,它 p/invokes 以不同的用户身份登录到远程机器。 Once you're logged on, you can perform the file copy normally.登录后,即可正常进行文件复制。

Also see this question: Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials —chiefly the answer that suggests using WNetUseConnection to connect to a UNC path on a remote machine with authentication.另请参阅此问题: 使用凭据从远程、不受信任的域访问共享文件 (UNC) — 主要是建议使用WNetUseConnection连接到远程计算机上的 UNC 路径并进行身份验证的答案。

Finally, just as a precaution, I feel like I must point out that whenever you start doing network file copies, you need to be very careful to check the availability of the network path before you just blindly start copying (the remote server could be down, the local machine could have lost its connection to the network, etc.).最后,作为预防措施,我觉得我必须指出,每当您开始进行网络文件复制时,您都需要非常小心地检查网络路径的可用性,然后再盲目开始复制(远程服务器可能已关闭) ,本地计算机可能已失去与网络的连接等)。 You might also need to consider handling cases where the network operation times out.您可能还需要考虑处理网络操作超时的情况。

You probably want to use FTP or SSH as a transport method to do something like this.您可能希望使用 FTP 或 SSH 作为传输方法来执行此类操作。 You just have to set up the right services on the server side to use these communication methods.您只需要在服务器端设置正确的服务即可使用这些通信方法。

Then you need a .Net library that speaks these protocols.然后你需要一个 .Net 库来说明这些协议。 Here is one:这是一个:

http://www.weonlydo.com/FtpDLX.NET/ftp-sftp-ssl-net.asp http://www.weonlydo.com/FtpDLX.NET/ftp-sftp-ssl-net.asp

It works, but I wasn't all that impressed by it for various reasons.它有效,但由于各种原因,我并没有对它印象深刻。 I'm sure there are more out there.我敢肯定还有更多。

And, yes, each of those protocols will challenge the connecting system for a username/password.而且,是的,这些协议中的每一个都会挑战连接系统的用户名/密码。

I would add an answer for the part "password identification on from and to" and if you have the admin permissions:我会为“从和到密码识别”部分添加一个答案,如果您有管理员权限:

use the administrator shares like this:像这样使用管理员共享:

\\server\c$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM