Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a .NET program from a mapped drive or shared folder

Tags:

c#

.net

unc

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.

like image 940
Rick Avatar asked Dec 11 '08 16:12

Rick


2 Answers

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

like image 160
Jon Galloway Avatar answered Oct 06 '22 01:10

Jon Galloway


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.

like image 23
Rob Prouse Avatar answered Oct 06 '22 02:10

Rob Prouse