如果我尝试在用户目录中编写代码,但一旦我尝试在Program Files中编写代码,它只是静默执行并且文件未被复制(没有例外)。如果我尝试复制C:\或C:\ Windows中的文件,则会捕获UnauthorizedAccessException。无法使用FileIOPermission复制具有适当权限的文件
你是否知道另一种获得在该目录中写入权限或使其以另一种方式工作的方式?
任何帮助非常感谢!谢谢
using(FileStream fs=File.Open(source, FileMode.Open)){ }
try
{
FileIOPermission fp = new FileIOPermission(FileIOPermissionAccess.Write,
AccessControlActions.Change, "C:\\Program Files\\MyPath");
fp.Demand(); //<-- no exception but file is not copied
File.Copy("C:\\Users\\teebot\\Documents\\File.xml","C:\\Program Files\\MyPath\\File.xml",true);
}
catch(SecurityExceptions)
{
throw(s);
}
catch(UnauthorizedAccessException unauthroizedException)
{
throw unauthroizedException;
}
当重新抛出异常时,只需使用throw; - 扔s;将会消除堆栈跟踪。 – 2008-10-01 21:45:13