tags:

views:

379

answers:

2

Seems that everything I do involves win services copying files across servers. I seem to get a lot of security & securityaccess type exceptions and never fully understand the causes. I am wondering if fileinfo or file.copy is a good solution or if there is a better. Is there a particular attribute I should be using or something to avoid these errors? Issue is not account or password related. example are \ipaddress\sharename \ipaddress\drive\path using domain accounts.

---Added Specific example.---
- I log on to server**A** as domain\username. (including domain name)
- I open file eplorer in the address bar enter \\server**B**\c$\folder hit enter, I right-click, create new file. No problem.
- I install service, go to properties select Log On, This Account and set the username as domain\username (including the domain name) same password I logged onto server**A** with. It accepts it no problem.
Application does a FileSystemWatcher on \\server**A** and copy to \\server**B** when changed to keep the config files in sync.

private void CopyNewFileToClone()  
{  
    FileInfo OriginalConfigFile = new FileInfo(Path.Combine(ConfigurationManager.AppSettings.Get("directoryToWatch"), ConfigurationManager.AppSettings.Get("fileToWatch")));  
    FileInfo CloneConfigFile = new FileInfo(Path.Combine(ConfigurationManager.AppSettings.Get("directoryToCopyTo"), ConfigurationManager.AppSettings.Get("fileToCopyTo")));
    FileInfo tmp = new FileInfo(Path.Combine(CloneConfigFile.DirectoryName,"~" + CloneConfigFile.Name));  

    OriginalConfigFile.CopyTo(tmp.FullName, true);  
    tmp.CopyTo(CloneConfigFile.FullName, true);  
    tmp.Delete();  
}

When I start the service I get Service cannot be started.
System.UnauthorizedAccessException: Access to the path '\\server**B**\C$\folder\filename' is denied.

+1  A: 

I use fileinfo to copy files from servers and it seems to work fine. If your sure it's not a account or password issue I would start looking at your DNS. If the network can't resolve what account is trying to access the network folder it won't matter if you are using a valid account. You may get lucky some/most of the time with cached accounts but there is no telling when it might not work and when it will work.

I would trace the network if you are getting a lot of broadcast messages for failed responses.

hipplar
A: 

This was due to Code Access Security policy. Ran
c:\Windows\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -af <path\application.exe>
and error resolved.

Adding as a installation step in all apps that need to write to HD, especially accross network via unc such as \server\share\file.