gpt4 book ai didi

powershell - 如何使用从 Powershell 中选择的文件调用 Windows 资源管理器?

转载 作者:行者123 更新时间:2023-12-01 13:50:35 25 4
gpt4 key购买 nike

我在编写我的第一个基本 PowerShell 实用程序时遇到了一个问题。我需要打开一个选择了文件的资源管理器窗口,但无法正确获取命令的语法。

我在 SO 上找到了这个答案,https://stackoverflow.com/a/12162855/957246并阅读了这篇关于外部命令的优秀博客,http://edgylogic.com/blog/powershell-and-external-commands-done-right/ - 但仍然无法让它工作。

这是我的示例脚本(test.ps1):

$myFile = "C:\Projects\Scripts\any.txt"
# The backslash-backticks are to escape the speech marks, once for PowerShell and then again for DOS
& "C:\Projects\Scripts\EchoArgs.exe" /select",\`"$myFile\`""
& "explorer" /select",\`"$myFile\`""

..这是调用它的批处理文件,以防万一我调用PowerShell脚本的方式有问题:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Projects\Scripts\test.ps1'"

我尝试了几种不同的组合,echoargs 的输出表明语法是正确的(假设资源管理器的参数字符串被视为单个字符串),但无论我尝试什么,都会在“我的文档”和参数上打开一个资源管理器窗口在某处被忽略/被扔掉。

MSDN 页面上的 explorer.exe 开关说逗号是一个空参数,也许这就是我需要通过不同地格式化命令来提供的东西?

最佳答案

使用 powershell 中的批处理命令有什么意义?你只会像那样混淆自己……和我。

$myFile = "C:\Projects\Scripts\any.txt"
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = Split-Path $myFile -Parent
$OpenFileDialog.FileName = Split-path $myfile -leaf
$OpenFileDialog.ShowDialog() | Out-Null

编辑

如果您需要继续探索探索路线,请尝试以下操作:
Start-Process -FilePath C:\Windows\explorer.exe -ArgumentList "/select, ""$myFile"""

关于powershell - 如何使用从 Powershell 中选择的文件调用 Windows 资源管理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32014711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com