简体   繁体   English

如何在 Windows 上创建文件夹的快捷方式?

[英]How to create a shortcut to a folder on Windows?

I have created shortcuts for executables and it works, but when I try to create one for a folder it does not work.我已经为可执行文件创建了快捷方式并且它可以工作,但是当我尝试为文件夹创建一个快捷方式时它不起作用。 It does create a shortcut, it is just not the right 'Target Type'.它确实创建了一个快捷方式,它只是不正确的“目标类型”。 Please take a look at the image below.请看下面的图片。 Instead of 'File', the target type should be 'File folder'.而不是“文件”,目标类型应该是“文件夹”。 The problem is that when I open the shortcut it asks me which program do I want to open the File with and it does not open the folder.问题是,当我打开快捷方式时,它会询问我要使用哪个程序打开文件,但它没有打开文件夹。

屏幕截图显示使用我的代码创建到文件夹的快捷方式的目标属性不正确

The function I'm using to create the shortcuts is the following我用来创建快捷方式的 function 如下

from win32com.client import Dispatch
import winshell
import os

def create_shortcuts(self, tool_name, exe_path, startin, icon_path):

    shell = Dispatch('WScript.Shell')
    shortcut_file = os.path.join(winshell.desktop(), tool_name + '.lnk')
    shortcut = shell.CreateShortCut(shortcut_file)
    shortcut.Targetpath = exe_path
    shortcut.WorkingDirectory = startin
    shortcut.IconLocation = icon_path
    shortcut.save()

I don't know if it's possible to set the 'Target Type'.我不知道是否可以设置“目标类型”。 I couldn't find a way to do it, but I do know there must be a way.我找不到办法,但我知道一定有办法。

If you want to use .Net "clr" (especially if you already require it):如果你想使用 .Net "clr"(特别是如果你已经需要它):

First run this... you will have to ship the output of this command this with your application首先运行这个...你必须将此命令的输出与你的应用程序一起发送

"c:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\TlbImp.exe" %SystemRoot%\system32\wshom.ocx /out:Interop.IWshRuntimeLibrary.dll

tlbimp.exe might even be in the path if you installed the Windows SDK in a fairly standard way.如果您以相当标准的方式安装了 Windows SDK,tlbimp.exe 甚至可能在路径中。 But if not, it's OK, you'll just ship the "assembly" (fancy word for interface-providing dll in .Net land) with your application.但如果没有,也没关系,您只需将“程序集”(在 .Net 领域中提供接口的 dll 的花哨词)与您的应用程序一起发送。

Then this code will work in python:然后这段代码将在 python 中工作:

import clr
sys.path.append(DIRECTORY_WHERE_YOU_PUT_THE_DLL)
clr.AddReference('Interop.IWshRuntimeLibrary')
import Interop.IWshRuntimeLibrary
sc = Interop.IWshRuntimeLibrary.WshShell().CreateShortcut("c:\\test\\sc.lnk")
isc = Interop.IWshRuntimeLibrary.IWshShortcut(sc)
isc.set_TargetPath("C:\\")
isc.Save()

.... the above code, with far too much modification and preamble, might even work with Mono. .... 上面的代码,经过太多的修改和序言,甚至可能适用于 Mono。

For future reference: I observed the described behavior in python 3.9.6 when creating a shortcut to a non-existing directory, which was easily fixed by incorporating os.makedirs() into the method.供将来参考:在创建不存在目录的快捷方式时,我观察到 python 3.9.6 中描述的行为,通过将os.makedirs()合并到方法中可以轻松修复。 I've added a method parameter to the version I'm using, so it can handle shortcuts to files and directories:我在我正在使用的版本中添加了一个方法参数,因此它可以处理文件和目录的快捷方式:

def create_shortcuts(self, tool_name, exe_path, startin, icon_path, is_directory=False):
    if is_directory:
        os.makedirs(exe_path, exist_ok=True) 

    shell = Dispatch('WScript.Shell')
    shortcut_file = os.path.join(winshell.desktop(), tool_name + '.lnk')
    shortcut = shell.CreateShortCut(shortcut_file)
    shortcut.Targetpath = exe_path
    shortcut.WorkingDirectory = startin
    shortcut.IconLocation = icon_path
    shortcut.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建指向 python 中文件夹的 windows 快捷方式 - how to create a windows shortcut to a folder in python 如何在Windows上创建conda环境快捷方式 - How to create a conda environment shortcut on Windows 如何为与 PyInstaller 捆绑的 Windows、MacOS 和 Linux 应用程序创建快捷方式图标 - How to create shortcut icons for Windows, MacOS and Linux applications bundled with PyInstaller 如何使用 setuptools Windows 安装程序在开始菜单中创建快捷方式 - How to create a shortcut in startmenu using setuptools windows installer 在python中的Windows中创建Web快捷方式 - Create web shortcut in windows in python 使用 Python 在 Windows 7 中创建快捷方式文件 - Create shortcut files in Windows 7 using Python 使用Run As Administrator创建Windows资源管理器快捷方式 - Create Windows Explorer shortcut with Run As Administrator 在 Windows 10 中使用带有 python 的 URL 创建快捷方式 - Create a shortcut using URL with python for windows 10 在Windows的virtualenv上创建安装的jupyter qtconsole快捷方式[7] - Create jupyter qtconsole shortcut installed on virtualenv on windows [7] 使用python3在windows中创建文件的快捷方式(.lnk) - create a shortcut(.lnk) of file in windows with python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM