将 app.config 加载到 AppDomain 中

2024-03-03

我无法将 App.Config 文件加载到应用程序域中。

我在用着

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path)

from Powershell 调用使用 App.config 的 .NET 程序集 https://stackoverflow.com/questions/835862/powershell-calling-net-assembly-that-uses-app-config但App.Config文件仍未加载。

我也尝试过重置缓存,如中所述使用 CurrentDomain.SetData("APP_CONFIG_FILE") 在 PowerShell ISE 中不起作用 https://stackoverflow.com/questions/13420545/using-currentdomain-setdataapp-config-file-doesnt-work-in-powershell-ise .

这是我的测试脚本:

$configFile = "{ActualPhysicalPath}\App.Config"
gc $configFile

Add-Type -AssemblyName System.Configuration
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)

[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $null)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configFile)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name

我总是获取存储在 machine.config 中的连接字符串,而不是 App.config 中的连接字符串。

如何在应用程序域中加载我的特定 App.Config 文件?


尝试移动你的SetData之前的声明GetField陈述。

对于 Windows 10 上的 PowerShell 5.0,以下提供的指导您引用的链接 https://stackoverflow.com/questions/13420545/using-currentdomain-setdataapp-config-file-doesnt-work-in-powershell-ise似乎有效:我能够检索两者应用程序设置 and 连接字符串.

Add-Type -AssemblyName System.Configuration

# Set this to the full path of your App.config
$configPath = "C:\Full\Path\To\App.config"

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configPath)
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)

[System.Configuration.ConfigurationManager]::AppSettings
[System.Configuration.ConfigurationManager]::ConnectionStrings
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 app.config 加载到 AppDomain 中 的相关文章

  • SMART 硬盘信息 Powershell [重复]

    这个问题在这里已经有答案了 我正在寻找一种通过可能的 powershell 来获取任何给定设备上硬盘驱动器的 SMART 数据的方法 我已经通过 CMD 和 powershell 尝试过 WMI 命令 但无法获得我正在寻找的适当数据 获取
  • 如何清除整个终端(PowerShell)

    我有一个问题 在powershell中使用clear或cls命令仅清除终端的可见部分 我想知道如何清除整个终端 顺便说一句 我使用 VSCode tl dr 问题是关于清零的both屏幕和回滚缓冲区 in the 综合终端 of 视觉工作室
  • Powershell 设置盖子关闭操作

    我想自动设置 Windows 7 在我的工作笔记本电脑上合上盖子时所执行的操作 因为每次登录时都会通过 GPO 重置该操作 我知道我可以在批处理脚本中使用 powercfg 命令来实现此目的 powercfg setacvalueindex
  • 在 Array Powershell 中获取新的附加值

    for Get All Files from the Folder FolderItems Get PnPFolderItem FolderSiteRelativeUrl FolderURL ItemType File Write Host
  • Powershell:启动进程不将参数传递给 cmd.exe

    这些是在 powershell 控制台 Windows 10 中运行的命令 username Username password Password securePassword ConvertTo SecureString password
  • Jenkins 执行 PowerShell 脚本

    我正在尝试从 Jenkins 运行 PowerShell 脚本 但它似乎完全忽略了执行策略 可以通过直接执行 powershell exe 或使用PowerShell插件 https wiki jenkins ci org display
  • 如何使用 Get-ChildItem 仅获取目录?

    我正在使用 PowerShell 2 0 我想通过管道输出某个路径的所有子目录 以下命令输出所有文件和目录 但我不知道如何过滤掉文件 Get ChildItem c mypath Recurse 我尝试过使用 Attributes获取属性
  • Powershell脚本命令持久化

    我开始学习 Powershell 并编写一个模块 psm1 来存储我的函数 然后我将这段代码插入到模块中 以便在修改模块时重新加载模块 function reload Remove Module init Import Module F S
  • 如何使用 R 或 PowerShell 从文本文件中提取数据?

    我有一个包含如下数据的文本文件 This is just text Username SOMETHI C Text Account DFAG Finish time 1 JAN 2011 00 31 58 91 Process ID 202
  • PowerShell 中的“nohup”相当于什么?

    如何模拟 unix 命令的行为nohup在 PowerShell 中 那是nohup my other nonblocking command 我注意到Start Job命令 但是语法对我来说有点不清楚 gt Start Job my ex
  • 如何在 PowerShell 中比较本地文件与 FTP 服务器上的文件的年龄并下载远程复制是否较新

    我正在编写一个 PowerShell 脚本 以帮助我为工作设置新电脑 希望这不仅仅是我一个人会使用 所以我正在尝试考虑所有事情 我在 FTP 服务器上保存了离线安装程序 java flash reader 等 如果本地副本尚未保存在创建的
  • 如何使用 Excel.UriLink.16 更改 Excel URL 的文件关联?

    我正在尝试更改文件关联 以便在另一个浏览器中打开 Excel 单元格中的 URL 根据使用 CMD ftype命令与Excel UriLink 16 我应该能够使用以下命令从 powershell 通过 cmd 执行此操作 To chang
  • Powershell日期类型无法找到

    我正在尝试使用PowerShell连接virustotal API 代码来自virustotal网站 我得到 无法找到类型 System Security Cryptography ProtectedData 错误信息 代码如下 funct
  • 我的 Powershell GUI 界面在打开网格视图时不断调整大小

    我目前正在构建一个复制到剪贴板工具 其中显示存储在文件夹中的 txt 文件列表 并且我使用 out gridview 来允许用户更好地选择和过滤列表 我已附上图片以供参考 单击加载 out gridview 的按钮后如何停止调整大小 Too
  • 尽管我可以在 DefinedTypes 中看到该类,但 Assembly.CreateInstance 返回 null

    我使用以下方法加载新的程序集并将类的实例创建到新的 AppDomain 中 private static object CreateInstanceFromBinary AppDomain appDomain string typeName
  • 填充 datagridview

    我正在开发一个表单 它将在所有连接的驱动器中搜索 PST 文件 我可以使用以下命令让它工作 Get PSDrive PSProvider filesystem get childitem root include pst r select
  • 使用 UTF-8 编码的 Powershell 字符串变量

    我检查了许多与此相关的问题 但找不到解决我的问题的东西 基本上 我想将 UTF 8 编码的字符串存储在变量中 然后使用该字符串作为文件名 例如 我正在尝试下载 YouTube 视频 如果我们打印视频标题 则会显示非英文字符 ytd这是you
  • 如何在管道函数中使用 Write-Progress?

    我正在尝试编写一个函数PowerShell接受管道输入 我想使用显示进度条Write Progress 管道中每个项目的增量 例如 function Write PipelineProgress Cmdletbinding Param Pa
  • Import-Csv - 成员已存在问题

    我必须将多个 CSV 文件合并为一个文件 每个 CSV 都有一个标题 其中一列标题是相同的 理想情况下 最终文件 all out csv 必须有一个标头 我运行 PowerShell 代码 Import Csv out 1 result c
  • 如何使用 PowerShell 扩展 ZIP 存档(UTF-8 文件名)

    我的 zip 存档有一个文件 P re No l txt 该 zip 可以通过 Windows 文件资源管理器 7 Zip 或我尝试过的任何其他工具很好地扩展 但我不知道如何从 PowerShell 中做到这一点 显然我已经尝试过展开 存档

随机推荐