gpt4 book ai didi

PowerShell:在默认的 txt 编辑器中打开文件

转载 作者:行者123 更新时间:2023-12-05 01:12:45 26 4
gpt4 key购买 nike

我正在尝试使用 PowerShell 在默认文本编辑器中打开非 txt 文件(在本例中为 hosts 文件)。

看到this Reddit post后,我有了一些进步,但 $txt_editor 结果总是返回 Notepad.exe,即使 Notepad++ 是我的 txt 文件的默认编辑器。

$hosts_file = "$env:windir\System32\drivers\etc\hosts"
$txt_editor = ((Get-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\txtfile\shell\open\command').'(Default)').trimend(" %1")
Start-Process -FilePath $txt_editor -Verb Runas -ArgumentList $hosts_file

这也返回 Notepad.exe:

(Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList' -Name a).a

如果我在注册表编辑器中查看上述位置,我确实看到 Notepad++ 列出了键 d,但我不知道如何仅查看注册表键来判断默认值是什么文本编辑器是,因为我在 Reddit 看到的两个解决方案都不起作用。

我使用的是 Windows 10,我正在寻找的解决方案将返回实际的默认文本编辑器文件位置,以便可以使用它来打开如上所示的文件。

最佳答案

Start 命令(Start-Process 的别名)将在其默认编辑器中启动任何文件。

start .\MyCoolbmp.bmp
#Opens in MSPaint

start .\SomeNotes.txt
#Opens in Notepad

start .\SomeJason.json
#Opens in Visual Studio, go ahead and grab a coffee...

如果我不得不猜测为什么你的不起作用,那是因为你提供的注册表项是系统使用的注册表项,而用户的默认编辑器存储在 Windows 7 及更高版本的 HKEY_CURRENT_USER 配置单元中,在此路径 Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice'

以下是相关的值:

$txtKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice"
Get-ItemProperty -Path $txtKey | tee-object -variable txtPath

Hash : noCJnt8yX5Y=
ProgId : VSCode.txt

这与在 HKCR:\Applications 中找到的信息有关,在那里可以找到真正的路径。

get-itemproperty Registry::\HKEY_CLASSES_ROOT\$($txtPath.ProgId)\shell\open\command


(default) : "C:\Program Files\Microsoft VS Code\Code.exe" "%1"
#...

如果您获取那个(默认)值,那么您现在已经获得了与文本文件关联的编辑器的真实路径。

要阅读有关该主题的更多信息,this blog post is pretty good并详细介绍了协会的运作方式。

关于PowerShell:在默认的 txt 编辑器中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61599183/

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