【问题标题】:How to fix the errors,name ' ' does not exist in the current context in c#如何修复错误,名称''在c#的当前上下文中不存在
【发布时间】:2021-07-31 18:26:29
【问题描述】:

我想开发一个控制台应用程序以获取截至check the status of IIS Application Pool 的 IIS 应用程序池状态。

我得到了类似的代码,并且在 DirectoryEntry 类中,当我添加给定的代码行时出现错误。(我只在该类中添加了以下行)

 appPools = new DirectoryEntry("IIS://" + ServerName + "/W3SVC/AppPools", UName, Pwd).Children;

谁能让我知道我应该包含哪些包以及我应该包含更多以消除错误。

我在 DirectoryEntry 类中遇到的错误:

         name 'appPools' does not exist in the current context.
         name 'Children' does not exist in the current context.
         the type or namespace name 'Pwd' could not be found(are u missing a using directory or an assembly reference)
         the type or namespace name 'ServerName' could not be found(are u missing a using directory or an assembly reference)
         the type or namespace name 'UName' could not be found(are u missing a using directory or an assembly reference)
         the modifier 'new' is not valid for this item
         identifier expected

由于我是 c# 新手,请大家帮帮我。

谢谢。

【问题讨论】:

  • edit您的问题包含您文件的完整源代码。看起来你把这行代码放在了不属于它的位置。
  • @Progman 已编辑。
  • 您忘记了 DirectoryEntry 类中字段 appPools 的类型。看起来应该是DirectoryEntries appPools = new Directory....。此外,您正在定义一个名为DirectoryEntry 的新类,同时还要创建一个DirectoryEntry 的实例。不确定您要对 DirectoryEntry 类做什么,但已经有一个类 System.DirectoryServices.DirectoryEntry。很可能您只想在那里删除您的DirectoryEntry 类。
  • @Progman 我提到stackoverflow.com/questions/37613513/… 有一个名为 DirectoryEntory 的类。这就是我使用它的原因,但我不明白该类的目的。如果我删除 DirectoryEntry 类,它会损害其中的功能program.cs?或者如果我删除了 DirectoryEntry 类,我必须如何修改 program.cs 类?
  • 当我删除 DirectoryEntry 类时,我仍然得到与上面提到的相同的错误,"if (appPool.Name.ToString().ToLower().Trim() == DT. Rows[i]["AppPoolSrvName"].ToString().ToLower().Trim())"。如果我从 program.cs 类的配置文件中删除获取服务器凭据和服务器名称的代码段,那么如何我修改它?(比如没有获取 Uname 和 Pwd,我可以从 appsettings.json 文件中获取进程名称吗?如果可能的话,如何实现它)或者有没有其他方法可以做到这一点?请问帮帮我,我真的很困惑

标签: c# console-application


【解决方案1】:

您应该安装NuGetSystem.DirectoryEntry,然后将using 添加到System.DirectoryServices.DirectoryEntry,然后它应该可以编译。

关于UName & Pwd,您只需要提供代表您的用户名和密码的字符串。

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多