dip s

dip s

  • 1.8k
  • 232
  • 24.4k

How to copy a file from local computer to remote computer

Jul 26 2021 11:45 AM

Hi all,

I want to copy a file (any file text, excel, image) from my local computer to cloud server (computer not in network).

Following is the code which i have tried. On local computer there is a file on c drive which I want to copy into remote server folder File_Upload which is present on c drive and shared folder.

With following code error is showing as "Network path was not found"

 // username and password for permission to access shared folder
// File_Upload folder is on c drive on remote computer and it is shared folder
var impersonationContext = new WrappedImpersonationContext(@"\\Remote_Server_IPAddress\c$\File_Upload", UserName, Password);
     
impersonationContext.Enter();

           
File.Copy(@"C:\File_Upload\Testing.txt", @"\\Remote_Server_IPAddress\c$\File_Upload\Testing.txt", true);

            
impersonationContext.Leave();


//=======================================

public sealed class WrappedImpersonationContext
    {
        public enum LogonType : int
        {
            Interactive = 2,
            Network = 3,
            Batch = 4,
            Service = 5,
            Unlock = 7,
            NetworkClearText = 8,
            NewCredentials = 9
        }

        public enum LogonProvider : int
        {
            Default = 0,  // LOGON32_PROVIDER_DEFAULT
            WinNT35 = 1,
            WinNT40 = 2,  // Use the NTLM logon provider.
            WinNT50 = 3   // Use the negotiate logon provider.
        }

        [DllImport("advapi32.dll", EntryPoint = "LogonUserW", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool LogonUser(String lpszUsername, String lpszDomain,
            String lpszPassword, LogonType dwLogonType, LogonProvider dwLogonProvider, ref IntPtr phToken);

        [DllImport("kernel32.dll")]
        public extern static bool CloseHandle(IntPtr handle);

        private readonly string _domain, _password, _username;
        private IntPtr _token;
        private WindowsImpersonationContext _context;

        private bool IsInContext
        {
            get { return _context != null; }
        }

        public WrappedImpersonationContext(string domain, string username, string password)
        {
            _domain = String.IsNullOrEmpty(domain) ? "." : domain;
            _username = username;
            _password = password;
        }

        // Changes the Windows identity of this thread. Make sure to always call Leave() at the end.
        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        public void Enter()
        {
            if (IsInContext)
                return;

            _token = IntPtr.Zero;
            bool logonSuccessfull = LogonUser(_username, _domain, _password, LogonType.NewCredentials, LogonProvider.WinNT50, ref _token);
            if (!logonSuccessfull)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            WindowsIdentity identity = new WindowsIdentity(_token);
            _context = identity.Impersonate();

            Debug.WriteLine(WindowsIdentity.GetCurrent().Name);
        }

        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        public void Leave()
        {
            if (!IsInContext)
                return;

            _context.Undo();

            if (_token != IntPtr.Zero)
            {
                CloseHandle(_token);
            }
            _context = null;
        }
    }

How can I resolved this issue? 

Thank you.


Answers (1)