Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6335827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:55:58+00:00 2026-05-24T18:55:58+00:00

I have a problem figuring out the permission set to get Directory.GetFiles() to work

  • 0

I have a problem figuring out the permission set to get Directory.GetFiles() to work in an assembly used by SQL Server reporting services.

Background

Basically, I use this assembly to perform two things:

  • read images from a file share
  • enumerate all images in a folder on that share.

Feature 1: Reading an image (works fine)

Reading images works using a custom permission set:

<PermissionSet class="NamedPermissionSet"
   version="1"
   Name="MyCustomImagePermissionSet"
    <IPermission class="FileIOPermission"
       version="1"
       Read="\\MyServer\MyFolder"/>
    <IPermission class="SecurityPermission"
       version="1"
       Flags="Assertion, Execution"/>
</PermissionSet>

and referencing this permission set in a custom code group:

<CodeGroup class="UnionCodeGroup"
   version="1"
   PermissionSetName="MyCustomImagePermissionSet"
   Name="MyImageCodeGroup"
      <IMembershipCondition class="UrlMembershipCondition"
         version="1"
         Url="C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\MyProject\MyProject.Reports.Code.dll"
       />
</CodeGroup>

(Snippets taken from RSPreviewPolicy.config)

In my code, I use an assertion before opening a FileStream:

private Image LoadOriginalImageByPath(string path)
{
    new FileIOPermission(FileIOPermissionAccess.Read, path).Assert();

    Image originalImage;

    using (var imageStream = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        originalImage = Image.FromStream(imageStream);
    }
    return originalImage;
}

Feature 2: Enumerating files in Directory (does not work)

Enumerating all the files in a subdirectory on the file server always results in a SecurityException:

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. 
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) 
   at System.Security.CodeAccessPermission.Demand() 
   at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption) 
   at System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption) 
   at System.IO.Directory.GetFiles(String path, String searchPattern) 
   at MyProject.Reports.Code.ImageTools.GetImagesFullPaths(String searchDirectory, Int32 registerNr) 
The action that failed was: 
Demand 
The type of the first permission that failed was: 
System.Security.Permissions.FileIOPermission 
The Zone of the assembly that failed was: 
MyComputer 

Here’s the code:

public static IEnumerable<string> GetImagesFullPaths(string searchDirectory, int registerNr)
{
    var pattern = string.Format("M{0:00000}?.jpg", registerNr);

    new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read, searchDirectory).Assert();
    return Directory.GetFiles(searchDirectory, pattern);
}

Workaround: Using FullTrust

The only way I found to get around the problem is to use FullTrust for the custom assembly. But obviously, that makes the admin uneasy.

The Question

What am I missing to get the Directory.GetFiles() work without using the FullTrust permission set?

Edit

Thanks to the accepted answer, the solution is adding the PathDiscovery attribute to the permission element resulting in

<PermissionSet class="NamedPermissionSet"
   version="1"
   Name="MyCustomImagePermissionSet"
    <IPermission class="FileIOPermission"
       version="1"
       Read="\\MyServer\MyFolder"/>
       PathDiscovery="\\MyServer\MyFolder"/>
    <IPermission class="SecurityPermission"
       version="1"
       Flags="Assertion, Execution"/>
</PermissionSet>

Thx!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:55:59+00:00Added an answer on May 24, 2026 at 6:55 pm

    Enumerating files requires FileIOPermissionAccess.PathDiscovery on the target path. You need to add this to your custom permission set.

    BTW, asserting the permission is essentially useless since the assert will not work unless your code already has the permission. (Assertions are only useful if you want your code to execute something that its calling code does not have permissions to do.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem figuring out exactly whats wrong here: -(void) fireShots { [laserBeam
I have a problem which I can't seem to be figuring out: I load
I am having trouble figuring out one implementation problem, I have one class, it
i have a little problem figuring out what request (MySQL) would suit this requirements:
I have been tasked with figuring out a state space for a problem based
Ok so i am having a problem with figuring this problem out. I have
i have problem with autorotate on iphone i set up in all classes -
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I am new to programming and having some problem figuring out nested loops. I
I have this problem in a bigger Project...... so I set up a 'Testpoject'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.