.Net - 在文件名中使用带有掩码的 FileIOPermission
我想使用文件名中的掩码对文件集应用 FileIOPermission,例如。在文件夹 C:\TMP 中的所有 txt 文件上:
[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();
}
}
}
这会引发 ArgumentException“路径中存在非法字符。”
怎么了?无论如何都可以实现吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查FileIOPermission Constructor的MSDN文档,它非常具体对于许多情况,包括“路径参数未指定文件或目录的绝对路径”,都会引发 ArgumentException
不幸的是,从字面上解释这意味着您不能在 FileIOPermission 中使用通配符。
如果没有实现您自己的权限类,您最好的办法就是引用 C:\TMP 目录。
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.