AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1821613
Accepted
YorSubs
YorSubs
Asked: 2023-12-14 08:05:03 +0800 CST2023-12-14 08:05:03 +0800 CST 2023-12-14 08:05:03 +0800 CST

将 .docx 与 swriter.exe LibreOffice 关联(使用 PowerShell)

  • 772

我想将 .docx 与 OpenOffice 关联

$extension = ".docx"
$executable = "$MyPrograms\LibreOffice\program\swriter.exe"
$fileType = "docxfile"
if (Test-Path $executable) {
    $elevated = @"
        cmd /c "assoc $extension=$fileType"
        cmd /c 'ftype $fileType="$executable" "%1" "%*"'
        New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
        Set-ItemProperty -Path "HKCR:\$fileType" -Name "(Default)" -Value "$fileType file" -ErrorAction Stop
"@
    Start-Process powershell -WindowStyle Hidden -Verb runAs -ArgumentList "-Command", $elevated
    Write-Host "`'$extension`' has been associated with `'$executable`'."
} else {
    Write-Host "`'$executable`' does not exist, so no association for `'$extension`' has been created."

这有一些问题:

  • 首先,它不能处理带有空格的文件,因此“This File.docx”会显示两个错误对话框“'This' was not found”,然后是“'File.docx' was not found”,所以ftype语法需要正确处理给出swriter.exe完整的文件名,我该怎么做?
  • 其次,即使我使用不带空格的名称“MyFile.docx”,所发生的只是控制台打开显示帮助输出,swriter.exe并且它不会打开应用程序,因此不知何故仅将文件传递到swriter.exe不足以获取要打开的程序。我该如何解决这个问题?
powershell
  • 2 2 个回答
  • 49 Views

2 个回答

  • Voted
  1. Best Answer
    Keith Miller
    2023-12-15T16:42:55+08:002023-12-15T16:42:55+08:00

    如果安装了libreoffice,则应该已经在下面定义了progid 。LibreOffice.DocxHKCR

    如果出于某种原因您不这样做,

    • HKCR\LibreOffice.Docx\shell\open\command
      • "C:\Program Files\LibreOffice\program\swriter.exe" -o "%1"

    纯Powershell等效

    从管理PowerShell控制台运行,这会创建相同的键/条目:

    $extension  = '.docx'
    $executable = 'D:\MyPrograms\LibreOffice\program\swriter.exe'
    $fileType   = 'LibreOffice.Docx'
    $Hive       = 'HKLM:\SOFTWARE\Classes'
    
    New-Item -Path "$HIve\$extension" -Value $fileType
    New-Item -Path "$Hive\$fileType" -Value "$fileType file"
    New-Item -Path "$Hive\$fileType\Shell\Open\Command" -Type ExpandString -Force -Value @"
    "$executable" -o "%1" 
    "@
    

    输出:

    >>
    
    
        Hive: HKEY_LOCAL_MACHINE\SOFTWARE\Classes
    
    
    Name                           Property
    ----                           --------
    .docx                          (default) : LibreOffice.Docx
    LibreOffice.Docx               (default) : LibreOffice.Docx file
    
    
        Hive: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\LibreOffice.Docx\Shell\Open
    
    
    Name                           Property
    ----                           --------
    Command                        (default) : "D:\MyPrograms\LibreOffice\pro
                                   gram\swriter.exe" -o "%1"
    
    
    PS C:\WINDOWS\system32>
    
    • 1
  2. YorSubs
    2023-12-17T00:06:30+08:002023-12-17T00:06:30+08:00

    有了@keith Miller的答案,我可以使我的原始代码工作以相关联的扩展名,swriter.exe如下所示:

    $extension = ".docx"
    $executable = "D:\MyPrograms\LibreOffice\program\swriter.exe"
    $fileType = "docxfile"
    if (Test-Path $executable) {
        $elevated = @"
            cmd /c "assoc $extension=$fileType"
            cmd /c 'ftype $fileType="$executable" -o """%1""" "%*"'
            New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
            Set-ItemProperty -Path "HKCR:\$fileType" -Name "(Default)" -Value "$fileType file" -ErrorAction Stop -Force
    "@
        Start-Process powershell -WindowStyle Hidden -Verb runAs -ArgumentList "-Command", $elevated
        Write-Host "`'$extension`' has been associated with `'$executable`'."
    } else {
        Write-Host "`'$executable`' does not exist, so no association for `'$extension`' has been created."
    }
    
    • 0

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve