如何在 Windows 上创建文件夹的快捷方式? [英] How to create a shortcut to a folder on Windows?

查看:58
本文介绍了如何在 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

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.

推荐答案

如果您想使用 .Net "clr"(特别是如果您已经需要它):

If you want to use .Net "clr" (especially if you already require it):

首先运行这个...你必须将这个命令的输出与你的应用程序一起发送

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 甚至可能在路径中,如果您以相当标准的方式安装了 Windows SDK.但如果没有,也没关系,您只需将程序集"(在 .Net 领域中提供接口的 dll 的花哨词)与您的应用程序一起发送.

tlbimp.exe might even be in the path if you installed the Windows SDK in a fairly standard way. But if not, it's OK, you'll just ship the "assembly" (fancy word for interface-providing dll in .Net land) with your application.

那么这段代码就可以在python中运行了:

Then this code will work in 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()

.... 上面的代码,经过太多的修改和序言,甚至可能适用于 Mono.

.... the above code, with far too much modification and preamble, might even work with Mono.

这篇关于如何在 Windows 上创建文件夹的快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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