tags:

views:

1936

answers:

5

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.

A: 

Have you turned on impersonation in the app.config file? It could be that the application is being run as the other machine, and not the user starting the application.

Dillie-O
I don't believe this applies to WinForm apps, only web apps.
Steven Behnke
+4  A: 

.NET 3.5 SP1 allows running applications off a network share. Previous versions did not allow it.

Jon Galloway
You should probably include that they did not allow it by default. They did allow it if you modified the security policies.
Steven Behnke
+3  A: 

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.

Rob Prouse
A: 

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.


I'm not sure how to turn on impersonation for the current user in app.config.

Rick
make sure you install sp1 for both VS and .net3.5
Joel Coehoorn
A: 

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.

Rick