我目前正在写一个方法,将做几件事情:
现在,如果我实现了传统嵌套,那么它是否可以工作。绝对为零的问题-但是,为了我所认为的更清洁的实现,它已经变成了一个可爱的错误。
语法:
bool result = false;
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal role = new WindowsPrincipal(user);
result = ((Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion .Version.Major > 6
&& role != null && role.IsInRole(WindowsBuiltInRole.Administrator)
? true : false);
但是我收到以下例外。
运算符
&&
不能应用于类型System.PlatformID
和的操作数bool
。
我真的不确定为什么它不起作用,应该这样做。我是错误地实现了逻辑还是什么,我真的很茫然。
此语法确实有效,但是当我将其转换为上述条件时,它却无效。
if(Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion
.Version.Major > 6)
{
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal role = new WindowsPrincipal(user);
if(role != null)
{
if(role.IsInRole(WindowsBuiltInRole.Administrator))
{
return true;
}
}
return false;
}
return false;
更新:
这是出现红色花体的地方,Visual Studio给出了上述错误:
PlatformID.Win32NT && Environment.OSVersion.Version.Major > 6
您的条件可以这样重写:
bool result = Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version.Major > 6 &&
role.IsInRole(WindowsBuiltInRole.Administrator);
请注意,您可以跳过'role'null检查,因为在您的情况下它永远不会为null。
编辑
就您的更新而言,问题在于此部分:
bool result = PlatformID.Win32NT; // <-- this part can't compile, it's not a boolean
我相信您要写的是:
bool result = Environment.OSVersion.Platform == PlatformID.Win32NT; // along with your other conditions
编辑2
既然您已经问过为什么您的示例不起作用(不确定您有什么错别字或到底发生了什么),但是此代码也可以编译(注意: 我不会这样写,只是说):
bool result = ((Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version.Major > 6
&& role != null && role.IsInRole(WindowsBuiltInRole.Administrator)
? true
: false));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句