需要使用批处理文件或Power Shell来设置程序的文件扩展名 [英] Need ot use a batch file or Power Shell to set a file extension to a program

查看:83
本文介绍了需要使用批处理文件或Power Shell来设置程序的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种编程方法来将关联设置为安装程序的一部分.

I need a programmatic method to set associations as part of an installer program.

我正在尝试设置文件扩展名,以便当用户双击该扩展名时,它将运行关联的程序,在这种情况下为c:\ temp \ test.bat

I am trying to set a file extension so that when a user double clicks on that extension it runs the associated program in this case c:\temp\test.bat

我尝试在.bat文件中使用assoc以及ftype命令,但是当文件.003(仅用于测试目的)时,它仍会打开已设置为该扩展名默认值的Unpacker程序.请参阅控制面板">程序">默认程序">设置关联".如果我使用此界面并更改与002 File关联的程序,则当我双击test.002时它将运行test.bat.

I have tried using the assoc and also the ftype commands in a .bat file but when the file .003 (just for testing purposes) it still opens the Unpacker program that was already set as the default for that extension. See image of Control Panel>Programs>Default Programs>Set Associations. If I use this interface and change the program associated as I did with 002 File then it works when I double click on test.002 it runs test.bat.

我需要在安装中包括此关联,因此我需要能够执行.bat文件中GUI所能执行的操作. .bat命令assoc和ftype似乎并未更改映像中的GUI,并且仍会启动Unpacker而不是test.batenter映像说明.

I need to include this association in an install so I need to be able to do what I can do in the GUI in a .bat file. the .bat commands assoc and ftype do not seem to make changes to the GUI in the image and also still launch Unpacker not test.batenter image description here

在此处输入图像描述

推荐答案

我也无法成功使用assocftype命令.我放弃了,直接与注册表合作.

I also failed to use the assoc and ftype commands successfully. I gave up and directly worked with registry.

这是一个注册表文件的示例,我将它分为两​​部分进行python关联.

Here's an example of a registry file I merged from 2 parts for python associations.

第一部分:HKEY_CLASSES_ROOT\.py:将扩展名与文件类型相关联. 第二部分:其余部分:将文件类型与各种操作相关联.

First part: HKEY_CLASSES_ROOT\.py: associates extension to the file type. Second part: the rest: associate the file type with the various actions.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.py]
@="Python.File"
"Content Type"="text/plain"

[HKEY_CLASSES_ROOT\Python.File]
@="Python File"

[HKEY_CLASSES_ROOT\Python.File\DefaultIcon]
@="C:\\Python34\\DLLs\\py.ico"

[HKEY_CLASSES_ROOT\Python.File\Shell]

[HKEY_CLASSES_ROOT\Python.File\Shell\Edit with IDLE]

[HKEY_CLASSES_ROOT\Python.File\Shell\Edit with IDLE\command]
@="\"C:\\Python34\\pythonw.exe\" \"C:\\Python34\\Lib\\idlelib\\idle.pyw\" -e \"%1\""

[HKEY_CLASSES_ROOT\Python.File\Shell\Edit with Pyscripter]
@="Edit with PyScripter"

[HKEY_CLASSES_ROOT\Python.File\Shell\Edit with Pyscripter\command]
@="\"C:\\Program Files\\PyScripter\\PyScripter.exe\" \"%1\""

[HKEY_CLASSES_ROOT\Python.File\Shell\Edit with Pythonwin]

[HKEY_CLASSES_ROOT\Python.File\Shell\Edit with Pythonwin\command]
@="C:\\Python34\\Lib\\site-packages\\Pythonwin\\Pythonwin.exe /edit \"%1\""

[HKEY_CLASSES_ROOT\Python.File\Shell\editwithidle]
"MUIVerb"="&Edit with IDLE"
"Subcommands"=""

[HKEY_CLASSES_ROOT\Python.File\Shell\editwithidle\shell]

[HKEY_CLASSES_ROOT\Python.File\Shell\editwithidle\shell\edit35-32]
"MUIVerb"="Edit with IDLE 3.5 (32-bit)"

[HKEY_CLASSES_ROOT\Python.File\Shell\editwithidle\shell\edit35-32\command]
@="\"C:\\Users\\dartypc\\AppData\\Local\\Programs\\Python\\Python35-32\\pythonw.exe\" -m idlelib \"%L\" %*"

[HKEY_CLASSES_ROOT\Python.File\Shell\open]

[HKEY_CLASSES_ROOT\Python.File\Shell\open\command]
@="\"C:\\WINDOWS\\py.exe\" \"%1\" %*"

[HKEY_CLASSES_ROOT\Python.File\shellex]

[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

您可以创建/生成这样的文件,然后使用REG IMPORT myregfile.reg将值添加到注册表中.

You can create/generate such a file and then use REG IMPORT myregfile.reg to add the values to the registry.

但是您需要为此运行提升权限的命令提示符,否则您将获得访问被拒绝".有很多链接到UAC/高架外壳/...供您搜索.

But you'll need to run an elevated command prompt for that or you'll get "access denied". There's a lot of links to UAC/elevated shells/... for you to search.

特定扩展名的最佳方法是:

The best way for your particular extension would be:

  1. 使用Windows手动关联文件
  2. 运行regedit&查找以上键(扩展名导致文件类型并搜索文件类型)
  3. 导出密钥,合并到单个.reg文件
  4. 随意破解:)
  1. associate the file manually using windows
  2. run regedit & look for the above keys (extension lead to file type and search for file type)
  3. export the keys, merge to a single .reg file
  4. hack it at will :)

应用程序:

当我不得不使用python Portable安装python关联时,我这样做了. Python可移植程序未设置文件关联,因为没有安装程序.因此,我将.py文件与此文件相关联,但是此脚本文件之外的其他参数在这里丢失了:

I did that when I had to install python associations using python portable. Python portable did not set the file associations because no installer. So I associated the .py file with it, but then arguments other that script file were missing here:

[HKEY_CLASSES_ROOT\Python.File\Shell\open\command]
@="\"C:\\WINDOWS\\py.exe\" \"%1\""

我刚刚编辑了该键并添加了缺少的%*,因此python可以处理参数.之后,我导出了reg文件,可以在其他计算机上使用它.

I just edited that key and added the missing %* so python works with arguments. After that I exported the reg file and I can use it on other machines.

这篇关于需要使用批处理文件或Power Shell来设置程序的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆