4

I have an c# winform application that runs under a local account but needs to monitor folders on a domain. I am using slightly modified code from here to copy the files and that works fine. Can similar code be used with the FileSystemWatcher set up impersonation so I can monitor a folder on a domain?

AdmSteck
  • 1,753
  • 15
  • 25
  • How should this be done? Do I just create the FileSystemWatcher object while impersonating the domain account? – AdmSteck Apr 01 '10 at 18:26

2 Answers2

7

Yes, there is a good impersonation class here, include this class in your project and than simply place your FileSystemWatcher within a using block like this:

using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
   ...

   <code that executes under the new context>

   ...
}
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • 1
    I wrapped the initialization of my FileSystemWatcher in a using block for the Impersonator like you suggested and it worked like a champ. Thanks. – AdmSteck Apr 01 '10 at 19:37
  • This does work, because the file handles are opened under the impersonated user context. Even after impersonation is reverted, the watcher will continue to watch. You will need to impersonate again to watch different files. – Sophit Oct 02 '14 at 20:52
1

You could:

Apply permissions to the domain folders that allow non-domain users access (i.e. Everyone.)

Logon with a domain account that has permissions and run the winform app.

Refactor the FileSystemWatcher code into a windows service and run it under a domian account with sufficient permissions.

Impersonate a domain account within the existing code, there are several good solutions on code project.

hollystyles
  • 4,979
  • 2
  • 36
  • 38