简体   繁体   English

C#将文件本地复制到远程或远程复制到本地

[英]C# Copy file local to remote or remote to local

I have custom component SSIS and I need copy the file from local to remote or from remote to local, but my remote address use credentials.我有自定义组件 SSIS,我需要将文件从本地复制到远程或从远程复制到本地,但我的远程地址使用凭据。

If I copy from remote credential location, it works, but opposite, it doesn't work.如果我从远程凭证位置复制,它可以工作,但相反,它不起作用。

Is there away option better?有没有更好的选择?

Copy from remote to local:从远程复制到本地:

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);
                    }

Copy file from local to remote将文件从本地复制到远程

if (isCreateDirectoryDestination)
                {
                    if (!Directory.Exists(Path.GetDirectoryName(fileDestination)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(fileDestination));
                    }
                }

                File.Copy(fileSource, fileDestination, isOverwrite);

You need to use impersonating你需要使用模拟

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.

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