Reputation: 1457
I have written a C# console app that will copy a backup from one server to another on a schedule every day. This works perfectly if I have logged into the share folder and my credentials are chached, however if my credentials have not been input on the share of the source server, I get an error in my code. What I need to do is have my app impersonate a login to my shared folder of the source, so I can grab the file and move it to its destination.
public static void CopyNewestBackup()
{
string sourcePath = @"\\source";
string targetPath = @"\\destination";
FileInfo newestFile = GetNewestFile();
string sourceFile = Path.Combine(sourcePath, newestFile.Name);
string destFile = Path.Combine(targetPath, newestFile.Name);
Console.Write("Copying " + newestFile.Name + " from " + sourcePath + " to " + destFile);
FileSystem.CopyFile(sourceFile, destFile, UIOption.AllDialogs);
//File.Copy(sourceFile, destFile, true);
}
How can I impersonate a login to the server to grab the file?
Upvotes: 1
Views: 8790
Reputation: 904
This MSDN article gives a good view on impersonation: http://msdn.microsoft.com/en-us/library/chf6fbt4.aspx
Upvotes: 1