建立连接并发送凭据以复制文件 [英] Establish a connection and send credentials to copy a file

查看:46
本文介绍了建立连接并发送凭据以复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否是我提出问题的合适论坛.

I wonder if this is the appropriate forum for my question.

我正在尝试使用File.Copy将一个位置/服务器中的文件复制到另一位置/服务器,但是似乎需要凭据,因为我收到以下错误消息:登录失败:未知的用户名或错误的密码" ;.

I am trying to use File.Copy to copy a file in one location/server to another but it seems that credentials are required because I get the following error message: "Logon failure: unknown user name or bad password".

如何首先建立连接并发送凭据?

How can I first establish a connection and send credentials?

此致,
豪尔赫·马尔多纳多(Jorge Maldonado)

Regards,
Jorge Maldonado

最诚挚的问候,乔治·马尔多纳多(JORGE MALDONADO)

Best regards, JORGE MALDONADO

推荐答案

您好豪尔赫(Jorge)

Hi Jorge,

您可以使用 小模仿课程

You could use the little impersonation class

using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
   // code that executes under the new context.
   File.Copy( x, y );
}

您可以使用P/调用WNetAddConnection2.尝试将其包装到IDisposable中并调用WNetCancelConnection2.

You can either use P/Invoke WNetAddConnection2.  Try to wrap it into an IDisposable and call WNetCancelConnection2.

using (new NetworkConnection(@"\\server\read", readCredentials))
using (new NetworkConnection(@"\\server2\write", writeCredentials)) {
   File.Copy(@"\\server\read\file", @"\\server2\write\file");
}

有关更多详细信息,请参阅

For more detailed information, please refer to How to provide user name and password when connecting to a network share

最诚挚的问候,

Kristin

Kristin


这篇关于建立连接并发送凭据以复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆