15

I am writing console application which will Copy file from my local disk to file server. This folder is protecting by username and password. File.Copy() method does not work. It gives permission error. I have looked to this code

I have tried it but it does not work. First it was written in VB but I have changed the code to C# but there are have some errors. I don't know what does this error mean. Maybe you can advise me other way coping file to protected File Server

with simple File.Copy(bla bla) it gives me "you have not permission"

when i converted VB code to C# it gived me error below: Attempted to read or write protected memory

I have found solution

You can Follow It

Community
  • 1
  • 1
AEMLoviji
  • 3,217
  • 9
  • 37
  • 61
  • 4
    I can advise you to post the errors. – John Saunders Feb 10 '11 at 13:21
  • Instead of "another way", let's try to get the way you've tried working. What kind of errors are you seeing when you convert that code? – Lasse V. Karlsen Feb 10 '11 at 13:22
  • when i converted code to C# it gived me error below: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. i am sure that i have changed it correctly but in one line i am not sure. this line is Dim admin_token As IntPtr i have changed it to IntPtr admin_token = new IntPtr(); – AEMLoviji Feb 10 '11 at 13:36
  • Related - http://stackoverflow.com/questions/17786037/copy-files-with-authentication-in-c-sharp – vapcguy Jan 24 '17 at 16:37

1 Answers1

18

You could use the little impersonation class I wrote some years ago:

using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
   // code that executes under the new context.
   File.Copy( x, y );
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291