Copy File on the Network using C#

  1. AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);  
  2.   
  3. IntPtr token;  
  4. LogonUser("username""domain""password", LogonType.LOGON32_LOGON_BATCH, LogonProvider.LOGON32_PROVIDER_DEFAULT);  
  5.   
  6. WindowsIdentity identity = new WindowsIdentity(token);  
  7.   
  8. WindowsImpersonationContext context = identity.Impersonate();  
  9.   
  10. try  
  11. {  
  12.     File.Copy(@"c:\temp\MyFile.txt", @"\\server\folder\Myfile.txt"true);  
  13. }  
  14. finally  
  15. {  
  16.     context.Undo();  
  17. }