2
下面的代码在Visual Studio在管理员帐户下运行时工作正常。 但是,当VS在非特权帐户下运行时,Save()方法会抛出UnauthorizedAccessException,但在这种情况下,我完全不明白为什么Demand()不会抛出SecurityException。当尝试写入文件时,需求不会抛出异常抛出UnauthorizedAccessException
public void SetLoggingLevel(string loggerRuleName, LoggingLevel loggingLevel)
{
foreach (XElement loggerRule in GetLoggerElements().Where(loggerRule => CompareWithLoggerRule(loggerRuleName, loggerRule)))
{
loggerRule.SetAttributeValue("minlevel", loggingLevel.ToString());
var permission = new FileIOPermission(FileIOPermissionAccess.Write, Source);
permission.Demand();
_configFile.Save(Source); //Writing to the xml-file
return;
}
throw new RuleNotFoundException("The rule not found.");
}
你尝试使用permission.Assert()吗? – Bobby5193
试过。相同的结果:UnauthorizedAccessException – EngineerSpock