Best practice to select and copy a file to a remote location? RRS feed

  • Question

  • I have a requirement where when a button is clicked the app connects to a remote pc and the user can browse to a folder on the c drive then copy a file to their pc (not on a LAN but a remote location)

    When using Remote desktop connection the details are (for example)
    Username: bob2\apple
    Password: password

    After doing a bit of research using WMI or Impersonation seems the best options. Here's where I'm at with the Impersonation approach..

    [DllImport("advapi32.DLL", SetLastError=true)]
            public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType,
                int dwLogonProvider, ref IntPtr phToken);
    private void button4_Click(object sender, EventArgs e)
                WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
                WindowsImpersonationContext wic = null;
                    IntPtr admin_token = new IntPtr();
                    if (LogonUser("bob2\apple", "","password",9, 0, ref admin_token) != 0)
                        wic = new WindowsIdentity(admin_token).Impersonate();
                        // NOT SURE ABOUT THIS BIT.....
                        File.Copy(@"", @"",
                        MessageBox.Show("Copy Succeeded");
                        MessageBox.Show("Copy Failed");
                catch(Exception se)
                    int ret = Marshal.GetLastWin32Error();
                    MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString());
                    if (wic != null)

    Would this be better suited in the general c# forum? It appears to be bombarded with malicious threads at the mo (these might have been removed by the time you look)



    • Edited by matleeds Tuesday, April 30, 2013 1:30 PM much better description
    • Moved by Mike Feng Sunday, May 12, 2013 8:49 AM
    Tuesday, April 30, 2013 8:39 AM

All replies