我正在使用这个 Impersonator 类将文件复制到具有访问权限的目录。
public void CopyFile(string sourceFullFileName,string targetFullFileName)
{
var fileInfo = new FileInfo(sourceFullFileName);
try
{
using (new Impersonator("username", "domain", "pwd"))
{
// The following code is executed under the impersonated user.
fileInfo.CopyTo(targetFullFileName, true);
}
}
catch (IOException)
{
throw;
}
}
这段代码工作得几乎完美。我面临的问题是,当 sourceFullFileName 是位于C:\Users\username\Documents等文件夹中的文件时,原始用户可以访问但模仿者没有。
尝试从此类位置复制文件时遇到的异常是:
mscorlib.dll 中出现“System.UnauthorizedAccessException”类型的未处理异常附加信息:对路径“C:\Users\username\Documents\file.txt”的访问被拒绝。