tags:

views:

251

answers:

2

Does anybody knows how to fix the exception that occurs when an .EXE file is run from the the Network Share. This application works fine when running from the local machine. I have been trying a lot to fix this issue for a while, but without any success.

Here is the simple code I am trying to test :

string AssessmentFilePath = @"\\abce.com\root\md\it\development\testfile\test.xml";
            INetworkFileShareReader reader = new NetworkFileShareReader();
            XPathDocument document = reader.GetAssessmentFileFromVendor(AssessmentFilePath);
            XPathNavigator nav = document.CreateNavigator();
            string rootPath = "/TABLE/APSOUTPUT";
            XPathNodeIterator iter = nav.Select(rootPath);

            int coutner = 0;
            while (iter.MoveNext())
            {
                coutner++;
                Console.WriteLine(coutner);

        }

of course, I have no problem running this code from my machine like : c:>abce.com\root\my.exe , assuming my my.exe is the output of this little cosole application.

I would really appreciate if someone can point me to the right direction :

Here is the snapshot of the Exception that I am getting :

Unhandled Exception: System.Security.SecurityException: Request for the permissi
on of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.
0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMa
rk& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessPermission.Demand()
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean
bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at SimpleNetworkShareApp.NetworkFileShareReader.GetAssessmentFileFromVendor(S
tring networkFileLocation)
   at SimpleNetworkShareApp.Program.Main(String[] args)
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.FileIOPermission
The first permission that failed was:
<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Vers
+3  A: 

It's the framework preventing you from running code from a remote location that could potentially cause problems. There are a number of solutions.

Look into Code Access Security.

Jay Riggs
+4  A: 

Running the application over the network will run it under a different security policy than running it from the local machine; in your case the application wants to alter files on the local machine but doesn't have permission.

To alter this you'll need to look into the .NET Framework Configuration tools that are included with the framework. That tool contains a "Runtime Security Policy" section where you can modify the permissions granted to network applications, or you can define a new policy to cover the specific network location.

Personally I'd lean towards creating a new Code Group for that particular share and granting it the necessary permissions; it will give you the ability to run the application without widening the security policies for all network applications.

To do this:

  1. Open the .NET Framework Configuration Tool to the "All_Code" code group
    1. Expand: .NET Framework (version) Configuration > My Computer > Runtime Security Policy > Machine > Code Groups > All_Code
  2. Right-click "All_Code" and select "New"
  3. Enter a name for the shared location
  4. Choose "URL" for the Condition, enter the network share path as the URL
  5. Define either a new permission set, or choose "Full Trust" to grant it all permission
STW
or if you got the option of using v4.0 (currently only in beta) you could update, which would fix the problem as well
Rune FS
@Rune FS: can you elaborate a little or provide a link? Not sure I follow what feature of .NET 4 you're referring to
STW