[英]C# Copy file local to remote or remote to local
我有自定义组件 SSIS,我需要将文件从本地复制到远程或从远程复制到本地,但我的远程地址使用凭据。
如果我从远程凭证位置复制,它可以工作,但相反,它不起作用。
有没有更好的选择?
从远程复制到本地:
using (new NetworkConnection(folder, new NetworkCredential(userName, password, domain)))
{
if (isCreateDirectoryDestination)
{
if (!Directory.Exists(Path.GetDirectoryName(fileDestination)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileDestination));
}
}
File.Copy(fileSource, fileDestination, isOverwrite);
}
将文件从本地复制到远程
if (isCreateDirectoryDestination)
{
if (!Directory.Exists(Path.GetDirectoryName(fileDestination)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileDestination));
}
}
File.Copy(fileSource, fileDestination, isOverwrite);
你需要使用模拟
IntPtr tokenHandle = new IntPtr(0);
tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(<userName>, <domain>, <password>, 2, 0, ref tokenHandle);
WindowsIdentity ImpersonatedIdentity = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext MyImpersonation = ImpersonatedIdentity.Impersonate();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.