【问题标题】:Powershell 2.0 Savefiledialog stylePowershell 2.0 保存文件对话框样式
【发布时间】:2017-03-31 10:42:47
【问题描述】:

我有一个 powershell 2.0 GUI,它有一个 SaveFileDialog 框,它看起来不像用于我的其他 MS Office windows 7 应用程序的 windows 7 保存文件对话框 - powershell 对话框包含一个垂直工具栏,称为 Places Bar,并且看起来像旧版本的 Windows。如何使我的 powershell GUI 中的保存文件对话框看起来像其他 Windows 7 应用程序保存文件对话框?

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    下面是原帖

    当您使用 PowerShell 2.0 时,Windows 将回退到旧式 Windows GUI,因为默认情况下,PowerShell 2.0 会加载 .Net 2.0。

    $PSVersionTable 在 PowerShell 2.0 中

    Name                           Value
    ----                           -----
    CLRVersion                     2.0.50727.5485
    BuildVersion                   6.1.7601.17514
    PSVersion                      2.0
    WSManStackVersion              2.0
    PSCompatibleVersions           {1.0, 2.0}
    SerializationVersion           1.1.0.1
    PSRemotingProtocolVersion      2.1
    

    $PSVersionTable 在 PowerShell 4 中

    Name                           Value                                                                                                                                                                                             
    ----                           -----                                                                                                                                                                                             
    PSVersion                      4.0                                                                                                                                                                                               
    WSManStackVersion              3.0                                                                                                                                                                                               
    SerializationVersion           1.1.0.1                                                                                                                                                                                           
    CLRVersion                     4.0.30319.42000                                                                                                                                                                                   
    BuildVersion                   6.3.9600.16406                                                                                                                                                                                    
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                                                                                                              
    PSRemotingProtocolVersion      2.2
    

    如果您安装了 .net CLR 3.5 或更高版本,您可以强制 PowerShell 2.0 加载比 2.0 更新的 CLR 版本,这将为您提供更新的样式文件对话框。

    将以下代码保存到批处理文件(例如 WindowsPowerShell3.5.cmd)中,其中 3.5 是您希望使用 PowerShell 加载的 CLR 版本。然后运行批处理文件以启动 PowerShell。将 powershell.exe 更改为 powershell_ise.exe 以将其应用于 PowerShell ISE 会话。

    @echo off
    :: http://stackoverflow.com/questions/7308586/using-batch-echo-with-special-characters
    if exist %~dp0powershell.exe.activation_config goto :run
    echo.^<?xml version="1.0" encoding="utf-8" ?^>                 > %~dp0powershell.exe.activation_config
    echo.^<configuration^>                                        >> %~dp0powershell.exe.activation_config
    echo.  ^<startup useLegacyV2RuntimeActivationPolicy="true"^>  >> %~dp0powershell.exe.activation_config
    echo.    ^<supportedRuntime version="v3.5"/^>                 >> %~dp0powershell.exe.activation_config
    echo.  ^</startup^>                                           >> %~dp0powershell.exe.activation_config
    echo.^</configuration^>                                       >> %~dp0powershell.exe.activation_config
    :run
    :: point COMPLUS_ApplicationMigrationRuntimeActivationConfigPath to the directory that this cmd file lives in
    :: and the directory contains a powershell.exe.activation_config file which matches the executable name powershell.exe
    set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=%~dp0
    %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe %*
    set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=
    

    原帖

    试试下面的代码,它基于来自Microsoft的示例代码:

    WPF

    Add-Type -AssemblyName PresentationFramework
    
    $dlg = New-Object 'Microsoft.Win32.SaveFileDialog'
    $dlg.FileName = "Document" # Default file name
    $dlg.DefaultExt = ".txt" # Default file extension
    $dlg.Filter = "Text documents (.txt)|*.txt" # Filter files by extension
    
    # Show save file dialog box
    $result = $dlg.ShowDialog()
    
    # Process save file dialog box results
    if ($result) {
      # Save document
      $filename = $dlg.FileName;
    }
    

    我猜你有使用 System.Windows.Forms 的代码,类似于下面的代码:

    Windows 窗体

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.forms")
    $dlg = New-Object System.Windows.Forms.SaveFileDialog
    $dlg.FileName = "Document" # Default file name
    $dlg.DefaultExt = ".txt" # Default file extension
    $dlg.Filter = "Text documents (.txt)|*.txt" # Filter files by extension
    
    # Show save file dialog box
    $result = $dlg.ShowDialog()
    
    # Process save file dialog box results
    if ($result) {
      # Save document
      $filename = $dlg.FileName
    }
    

    【讨论】:

    • 使用 3.5 对我不起作用 - 我收到有关 .Net 的错误:未安装 .NET Framework 的 v2.0.50727 版本,需要运行版本 1 的 Windows PowerShell。跨度>
    • 虽然 PSVersionTable 显示 CLRVersion 为 2.0.50727.5485。
    • 查看这篇 SO 文章,了解如何判断您安装了哪些版本的 .net CLR:stackoverflow.com/a/3495491/1368849
    • 根据我的注册表显示 v2.0.50727、v3.0、v3.5 和 v4。所有这些都给出相同的 .net 版本错误。
    • 在我搜索您的错误时发现的此链接中查看可能的解决方案:mikefrobbins.com/2012/07/24/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多