问题
I have a Windows Service created in .NET 4.
I need to access a file from antoher server and I get an exception:
"UnauthorizedAccessException was caught" - "Access to the path '(path)' is denied."
If I make a new Windows Forms application which accessed that file, it works. The Windows Service runs as Local System.
回答1:
Apparently your machine account does not have permissions to access remote file share. You have to grant rights to the machine account <DOMAIN>\<machine_name>$
on both file share level as well as file system level. For share permissions use fsmgmt.msc
, for file system you can do it using cacls
command.
Alternative solution is to impersonate a valid user account that already has permission to this directory.
来源:https://stackoverflow.com/questions/8047656/windows-service-unauthorizedaccessexception