当 Visual Studio 在管理员帐户下运行时,下面的代码可以正常工作。但是当VS在非特权帐户下运行时,Save()方法会抛出UnauthorizedAccessException,但是在这种情况下我很不明白为什么Demand()不会抛出SecurityException。
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.");
}