views:

59

answers:

1

Hello!

I would like to apply FileIOPermission on set of files using mask in file name, ex. on all txt files in folder C:\TMP:

[type: FileIOPermission(SecurityAction.PermitOnly, Read = @"C:\TMP\*.txt")]
class SomeClass
{
    static void testPermissions()
    {
        Console.WriteLine("allowed action");
        File.OpenRead(@"C:\TMP\1.txt"); // <--- here goes exception
        Console.WriteLine("denied action");
        try
        {
            File.Create(@"C:\TMP\2.txt");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            Console.ReadKey();
        }
    }
}

This throws ArgumentException "Illegal characters in path."

What is wrong? Is it possible to achieve anyway?

+2  A: 

Checking the MSDN documentation for the FileIOPermission Constructor, it is quite specific that an ArgumentException will be thrown for a number of conditions including "The path parameter does not specify the absolute path to the file or directory"

Unfortunately interpreting this literally implies that you can't use wildcards with FileIOPermission.

Short of implementing your own permission class, the best you could do would be just to refer to the C:\TMP directory instead.

David Gardiner