[英]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.