I have a network shared drive ("\serveur\folder") on which I would like to copy file. I can write on the drive with a specific user ("user"/"pass"). How can I access the shared drived with write privilege using C#?
A:
Create a user with privileges to write to the network drive and use impersonation within the c# to use that user when accessing the drive.
Am
2009-09-16 10:49:52
+2
A:
Untested code, but it will be similiar to:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity identity = new WindowsIdentity(username, password);
WindowsImpersonationContext context = identity.Impersonate();
try
{
File.Copy(@"c:\temp\MyFile.txt", @"\\server\folder\Myfile.txt", true);
}
catch
{
context.Undo();
}
Mitch Wheat
2009-09-16 10:56:09