3

I have an application that copies files that could be located anywhere on the local hard drive to a directory on a network share. The network directory is only accessible for a special domain account. I generally solved the problem by using an Impersonator-Class in this way:

using(new MyImpersonator()
{
  File.Copy(source, destination);
}

But this causes problems when I want to copy a file from a special directory, for example the Windwos-Dektop. Then I get an UnauthorizedAccessException because the domain account that has rights to write on the network share doesn't have the rights to read from the local Desktop. Giving the special domain account the right to read from every folder on the local computer during installation is not an option because of restrictions from my costumer.

A workaround for me is to create a temp directory in C:\ProgramData\MyApp that could be accessed by both, the local and the network user and copy the files at first to this temp directory and then with the special domain account to the network share. But this is not very performant.

Does anyone have a smarter solution for this problem?

Community
  • 1
  • 1
Lightbringer
  • 765
  • 1
  • 4
  • 16
  • What the maximum size of the files? You could read the files in memory before using the MyImpersonator implementation. – Jeroen Heier Jun 17 '16 at 05:46
  • If your in a network you should have an network account group for admins, just add your impersonator account there then you should have read/write access to machine across the network. as far as coding is concern there are no ways you can bypass the security of a network without turning your program to a malware. – Dr. Stitch Jun 17 '16 at 05:49
  • You don't have to change permission on any folder. Just change the permission on each of the files you want to copy to give read/delete access to the impersonated account, then start impersonation. That said, moving a file to a temporary folder should be nearly instant as long as the source and destination folder are on the same partition – Kevin Gosse Jun 17 '16 at 05:49
  • @Jeroen: Typical file sizes are 20-30 MB but up to 400MB is also possible. I will give that approach a try. – Lightbringer Jun 17 '16 at 06:29
  • @ Dr. Stitch: As I wrote, giving the impersonator account admin privileges on every machine unfortunately is not an option @KooKiz: Changing the file permissions sounds good but is also not an option because the typical users don't have the rights to change file permissons on their machines. – Lightbringer Jun 17 '16 at 06:36
  • Look also at [this](http://stackoverflow.com/questions/2809514/outofmemoryexception-when-i-read-500mb-filestream) SO question and answers when implementing. – Jeroen Heier Jun 17 '16 at 06:43

0 Answers0