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..
Code:
[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();
}
}
thanks,