Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

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

$
0
0

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)
Computer: abcd.dyndns.org:1234
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;
            try
            {
                IntPtr admin_token = new IntPtr();
               
                if (LogonUser("bob2\apple", "abcd.dyndns.org:1234","password",9, 0, ref admin_token) != 0)
                {   
                    wic = new WindowsIdentity(admin_token).Impersonate();

                    // NOT SURE ABOUT THIS BIT.....
                    File.Copy(@"", @"",
                        true);
                    MessageBox.Show("Copy Succeeded");
                }
                else
                {
                    MessageBox.Show("Copy Failed");
                }
             }
            catch(Exception se)
            {
                int ret = Marshal.GetLastWin32Error();
                MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString());
                MessageBox.Show(se.Message);
            }
            finally
            {
                if (wic != null)
                    wic.Undo();
            }
        }

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)

cheers,

Mat



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>