title: Impersonating user for local file access in C#
body: <p>The situation I'm trying to address is this: I'm writing an application which multiple users will have access to. Access is restricted based on Windows permissions for folders - users will be granted access to the folder containing the application if needed.</p>
  
  <p>For better or worse, the application stores its data in files on the same network as the application. I don't want users to be able to edit the data directly, so I plan to restrict access to the data files.</p>
  
  <p>The approach I've been trying to use is then to have a 'service user' which does have read/write access to the data, and to use impersonation within the application to 'login' as the service user, perform required read/write, and return to the original user.</p>
  
  <p>I've had a few different attempts at this without luck. Perhaps the simplest/most promising is based on Mark Johnson's answer here:</p>
  
  <p><a href="https://stackoverflow.com/questions/125341/how-do-you-do-impersonation-in-net">How do you do Impersonation in .NET?</a></p>
  
  <p>I use it as follows:</p>
  
  <pre><code>            using (new Impersonation(serviceAccount.Domain, serviceAccount.UserName, serviceAccount.Password))
              {
                  DoImport(app);
              }
  </code></pre>
  
  <p>where 'DoImport(app)' performs the reading of the data.</p>
  
  <p>However, this gives an error 'Access to the path '...' is denied'. I'm trying to run this locally (the path is C:...) where I've restricted access to the path for the user I'm logged into but the user I'm trying to impersonate with has access.</p>
  
  <p>Is there something I'm doing wrong here? Is there a better way to achieve what I'm after?</p>
  
  <p>Thanks,</p>
  
  <p>Andrew</p>