8

如何指定用户名和密码以便我的程序打开文件进行读取?需要访问文件的程序是从一个对文件所在文件夹没有读取权限的帐户运行的。程序是用 C# 和 .NET 2 编写的,在 XP 下运行,文件位于 Windows Server 2003 机器上。

4

3 回答 3

12

您想模拟一个有权访问该文件的用户。

我建议使用这样的类 - http://www.codeproject.com/KB/cs/zetaimpersonator.aspx。它隐藏了所有令人讨厌的模拟实现。

using (new Impersonator("myUsername", "myDomainname", "myPassword"))
{
  string fileText = File.ReadAllText("c:\test.txt");
  Console.WriteLine(fileText);
}
于 2008-10-30T08:41:05.577 回答
6

我用过 Nuget 包NuGet Gallery | Simple Impersonation Library 1.1.0但还有其他;在模仿中搜索其他人。

使用交互式登录处理文件结构的示例用法:

using (Impersonation.LogonUser("{domain}",
                               "{UserName}", 
                               "{Password}", 
                               LogonType.Interactive))
{
     var directory = @"\\MyCorpServer.net\alpha\cars";

     Assert.IsTrue(Directory.Exists(directory));
}

詹姆斯在下面的回答是在 Nuget 之前以及他后来在 Nuget 上下载最多的软件包之前。讽刺吧?

于 2016-02-18T21:41:37.513 回答
0

您可以冒充具有必要权限的用户。MSDN 上有一篇文章描述了如何执行此操作。

于 2008-10-30T08:21:43.867 回答