You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在PowerShell中使用Ookii.Dialogs.WinForms的InputDialog遇加载错误求助

问题解决:Ookii.Dialogs.WinForms InputDialog 无法加载 System.Resources.Extensions 程序集

问题背景

在Windows 11 Home 23H2、.NET Framework 4.8.09032、PowerShell 5.1环境下,使用Ookii.Dialogs.WinForms.dll(net462版本)扩展消息框功能时,TaskDialog可正常运行,但调用InputDialog的ShowDialog方法时,提示无法加载System.Resources.Extensions程序集(版本4.0.0.0),手动导入该DLL也无法解决。

相关代码:

# Loading all possible assemblies (Yes, I know this is overkill - but trying to find what works)
Add-Type -AssemblyName System, System.Windows.Forms, System.Drawing, PresentationCore, Presentationframework, Microsoft.VisualBasic, WindowsFormsIntegration
[System.Windows.Forms.Application]::EnableVisualStyles()

# Load Ookii.Dialogs.WinForms dll into session (.dll from Nuget Package - net462)
Add-Type -Path "C:\Ookii.Dialogs.WinForms.dll"

# Build/Show cleaner InputDialog (Not the ugly Visual Basic one)
$InputDialog = [Ookii.Dialogs.WinForms.InputDialog]::new()
$InputDialog.WindowTitle = 'Input Dialog'
$InputDialog.MainInstruction = 'This is the main instruction area.'
$InputDialog.Content = 'This is me testing the content area. Just adding more info.'
$InputDialog.Input ='Placeholder'
$InputDialog.ShowDialog()

原因分析

Ookii.Dialogs.WinForms的net462版本依赖的System.Resources.Extensions 4.0.0.0在.NET Framework 4.8环境中存在版本不匹配问题,PowerShell 5.1的程序集绑定策略无法自动解析该差异。

解决方案

方案1:更换适配.NET Framework 4.7.2+的Ookii版本

从NuGet下载Ookii.Dialogs.WinForms的.NET Framework 4.7.2或更高版本的包(避免netstandard/netcore版本),替换现有DLL后重新加载。这类版本已适配.NET Framework 4.8的程序集依赖。

方案2:添加程序集绑定重定向

在PowerShell会话中手动添加绑定重定向规则,让系统自动将对4.0.0.0版本的请求重定向到当前环境存在的版本:

# 加载配置相关程序集
Add-Type -AssemblyName System.Configuration

# 打开当前程序集的配置文件
$config = [System.Configuration.ConfigurationManager]::OpenExeConfiguration([System.Reflection.Assembly]::GetExecutingAssembly().Location)
$section = $config.GetSection("runtime")
if (-not $section) {
    $section = New-Object System.Configuration.ConfigurationSection("runtime")
    $config.Sections.Add("runtime", $section)
}

# 添加System.Resources.Extensions的重定向规则
$redirectXml = @"
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Resources.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.3.0" /> <!-- 替换为你系统中实际的版本号 -->
  </dependentAssembly>
</assemblyBinding>
"@

$section.SectionInformation.SetRawXml($redirectXml)
$config.Save([System.Configuration.ConfigurationSaveMode]::Modified)
[System.Configuration.ConfigurationManager]::RefreshSection("runtime")

# 重新加载Ookii DLL并测试
Add-Type -Path "C:\Ookii.Dialogs.WinForms.dll"

$InputDialog = [Ookii.Dialogs.WinForms.InputDialog]::new()
$InputDialog.WindowTitle = 'Input Dialog'
$InputDialog.MainInstruction = 'This is the main instruction area.'
$InputDialog.Content = 'This is me testing the content area. Just adding more info.'
$InputDialog.Input ='Placeholder'
$InputDialog.ShowDialog()

提示:newVersion的值需替换为你系统中实际存在的版本号。可在C:\Windows\Microsoft.NET\Framework64\v4.0.30319目录下找到System.Resources.Extensions.dll,右键查看属性的详细信息获取版本。

方案3:复制对应版本DLL到Ookii目录

找到系统中匹配版本的System.Resources.Extensions.dll(.NET Framework 4.8安装目录下),复制到Ookii.Dialogs.WinForms.dll所在目录,再重新加载程序集。

验证

执行任一方案后,重新运行InputDialog代码,应可正常显示对话框,不再出现程序集加载错误。

内容的提问来源于stack exchange,提问作者AnonPowershellGuy1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 05:05:20