问题
I have written a C# Windows Forms application to merge the files and folders from a remote folder on one machine ("source" folder is a mapped drive - "Z:\folder") with another remote folder on a different machine ("destination" folder is a UNC path to a shared folder - "\\computername\sharedfolder"). I have Full permissions to both folders. When I run the program on my local machine, it works fine, but when I try to run it from from within the source folder it fails with a security exception.
The failure occurs when calling the DirectoryInfo constructor for the destination folder (i.e., DirectoryInfo(@"\\computername\sharedfolder"). I assume the problem is because I am running the program from a mapped drive. Any workarounds?
The specific exception is: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
UPDATE
okay, I got my application into Visual Studio 2008 (it was previously coded in 2005), targeted the .NET 3.5 framework, compiled and tried again.
I got the exact same error.
UPDATE - RESOLUTION
I tried it with .NET 3.5, and it didn't work, then I noticed that you said 3.5 SP1. The service pack is definately needed.
Problem solved. Thank you.
回答1:
.NET 3.5 SP1 allows running applications off a network share. Previous versions did not allow it.
回答2:
You need to enable FullTrust permissions for the application. .NET applications that run on a network share are given Local Intranet security permissions and thus run in a sandbox.
Here is a batch file that I wrote for one of our testing apps that runs off the network. It should get you up and running with minor modifications.
@ECHO OFF
SET CASPOL=%windir%\Microsoft.NET\Framework\v2.0.50727\CasPol.exe
CLS
%CASPOL% -pp off
%CASPOL% -m -ag 1.2 -url file://server/directory/* FullTrust
As stated above, .NET 3.5 removes this behaviour.
来源:https://stackoverflow.com/questions/359978/run-a-net-program-from-a-mapped-drive-or-shared-folder