2010-03-31 36 views
0

我想在文件名中使用掩碼將ext文件應用於一組文件,在文件夾C:\ TMP中的所有txt文件上:.net - 在文件名中使用帶有掩碼的FileIOPermission

[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。」

出了什麼問題?無論如何可以實現嗎?

回答

2

檢查的FileIOPermission Constructor MSDN文檔,它是非常具體的是一個ArgumentException將被拋出了一些條件,包括「path參數不指定文件或目錄的絕對路徑」

可惜解釋這實際上意味着你不能在FileIOPermission中使用通配符。

實現自己的權限類的最大缺點是,你可以做的最好的只是引用C:\ TMP目錄。