Powershell 2.0 保存文件对话框样式



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

下面的原始帖子

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

PowerShell 2.0 中的$PSVersionTable

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

PowerShell 4 中的$PSVersionTable

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%System32WindowsPowerShellv1.0powershell.exe %*
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=

原始帖子

尝试下面的代码,它基于 Microsoft 中的示例代码:

可湿性工作基金会

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的代码,类似于下面的代码:

窗口窗体

[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
}

最新更新