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