C#使用权限将文件从一台服务器复制到另一台服务器

时间:2013-02-08 13:19:07

标签: c# impersonation fileinfo

我编写了一个C#控制台应用程序,它会每天按计划将备份从一台服务器复制到另一台服务器。如果我已登录到共享文件夹并且我的凭据被加密,这可以很好地工作,但是如果我的凭据没有在源服务器的共享上输入,我的代码中出现错误。我需要做的是让我的应用程序模拟登录到源的共享文件夹,这样我就可以抓取文件并将其移动到目的地。

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);
    }

如何模拟登录服务器以获取文件?

1 个答案:

答案 0 :(得分:1)

这篇MSDN文章提供了一个关于模仿的好视图: http://msdn.microsoft.com/en-us/library/chf6fbt4.aspx