How to present credentials in order to open file?

后端 未结 3 987
广开言路
广开言路 2020-12-06 12:55

How do I specify the username and password in order for my program to open a file for reading? The program that needs to access the file is running from an account that does

3条回答
  •  生来不讨喜
    2020-12-06 13:18

    You want to impersonate a user who does have the rights to access the file.

    I recommend using a class like this - http://www.codeproject.com/KB/cs/zetaimpersonator.aspx. It hides all the nasty implementation of doing impersonation.

    using (new Impersonator("myUsername", "myDomainname", "myPassword"))
    {
      string fileText = File.ReadAllText("c:\test.txt");
      Console.WriteLine(fileText);
    }
    

提交回复
热议问题