python桌面快捷图标_python创建桌面快捷方式(pythonwin)及从快捷方式获取文件路径...

从网上搜到些代码,分享一下,如何创建一个文件的快捷方式;反过来如何从快捷方式中提取出文件的路径,详细代码如下:# coding:gbk

# GetLink.py

# hbxcyz.cn

import os

import pythoncom

from win32com.shell import shell

from win32com.shell import shellcon

#从.lnk文件中获取文件路径

def GetpathFromLink(lnkpath):

shortcut = pythoncom.CoCreateInstance(

shell.CLSID_ShellLink, None,

pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)

shortcut.QueryInterface( pythoncom.IID_IPersistFile ).Load(lnkpath)

path = shortcut.GetPath(shell.SLGP_SHORTPATH)[0]

return path

#创建快捷方式

def CreateLnkpath(filename,lnkname):

shortcut = pythoncom.CoCreateInstance(

shell.CLSID_ShellLink, None,

pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)

shortcut.SetPath(filename)

if os.path.splitext(lnkname)[-1] != '.lnk':

lnkname += ".lnk"

shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(lnkname,0)

#创建url快捷方式

def CreateURLShortcut(url,name):

shortcut = pythoncom.CoCreateInstance(

shell.CLSID_InternetShortcut,None,

pythoncom.CLSCTX_INPROC_SERVER,shell.IID_IUniformResourceLocator)

shortcut.SetURL(url)

if os.path.splitext(name)[-1] != '.url':

name += '.url'

shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(name,0)

#从.url快捷方式获取url连接地址

def GetURLFromShortcut(url):

shortcut = pythoncom.CoCreateInstance(

shell.CLSID_InternetShortcut,None,

pythoncom.CLSCTX_INPROC_SERVER,shell.IID_IUniformResourceLocator)

shortcut.QueryInterface(pythoncom.IID_IPersistFile).Load(url)

url = shortcut.GetURL()

return url

#获取桌面路径

def GetDesktoppath():

ilist = shell.SHGetSpecialFolderLocation(0,shellcon.CSIDL_DESKTOP)

dtpath = shell.SHGetPathFromIDList(ilist)

#dtpath = dtpath.decode('gbk')

return dtpath

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值