通过脚本为软件分配文件类型

问题描述 投票:0回答:1

希望在 Windows 11 中通过脚本(最好通过 PowerShell 或 .reg)将某些文件类型分配给软件。

此 PowerShell 代码一年多前运行良好:

cmd /c "assoc .rhistory = rhistory_file"
cmd /c "ftype rhistory_file = "`"C:\Program Files\RStudio\rstudio.exe`" `"%1`"""

也尝试过这样的事情:

$RStudioPath = "C:\Program Files\RStudio\rstudio.exe"
$FileType = ".rhistory"
$FileAssociation = [Microsoft.Win32.Registry]::ClassesRoot.CreateSubKey($FileType)
$FileAssociation.SetValue("", "RStudio.rhistory")
$FileAssociation.CreateSubKey("shell")
$Command = $FileAssociation.CreateSubKey("shell\open\command")
$Command.SetValue("", "`"$RStudioPath`" `"%1`"")

有人可以帮忙吗? 谢谢你。

powershell registry file-extension file-type
1个回答
0
投票

假设您已运行它们以海拔,即作为管理员,您的命令看起来是正确的原则上

但是,在每个文件名扩展的基础上,可以通过文件资源管理器在给定计算机上实施特定于用户的覆盖,即:

  • 如果当前用户以交互方式选择始终使用特定程序打开具有给定扩展名的文件,请在文件资源管理器中右键单击此类文件,选择

    Open with > Choose another app
    ,选择感兴趣的应用程序,然后单击于
    Always

  • 通过我不清楚的机制,一些应用程序 - 特别是Visual Studio Code - 设法安装这样的持久覆盖,即使您单击

    Just once
    ,甚至只是拖动文件到打开的Visual Studio Code 窗口。

无论哪种方式,为了撤消这样的覆盖,您需要将其从注册表中删除,网址为

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\<.extension>
;在您的情况下(使用 PowerShell 的
HKCU:
驱动器来引用
HKEY_CURRENT_USER
配置单元):

#requires -RunAsAdmin
# Assumes that the key exists.
Remove-Item -Recurse HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.rhistory

需要注意的是,每当您(当前用户)选择再次使用 Visual Studio Code 等应用程序打开具有感兴趣扩展名的文件时,您都必须重复上述操作 - 即使有意这样做 仅临时

© www.soinside.com 2019 - 2024. All rights reserved.