C#获取桌面快捷方式路径并读取其目标路径

转载:https://blog.csdn.net/qiqingli/article/details/92798661
C#获取桌面快捷方式路径并读取其目标路径
主要核心:

1.查找桌面对应的路径

2.查找桌面路径下的所有快捷方式

3读取该快捷方式下对应的目标路径(程序启动的exe文件的路径)

(利用 com组件 Windows Script Host Object Model)

核心代码如下:

using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
 
namespace GetApplicationInstrallPath
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnReLoad_Click(object sender, EventArgs e)
        {
 
            string desktopPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
            //string deskpath2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
            //string deskpath3 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Templates);
            string deskpath4 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu);//开始菜单项
            string deskpath6 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
            string deskpath8 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Recent);//只记录文档,运行的程序记录没有记录
            string deskpath7 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Programs);//开始最菜单的程序
 
            string dekpath5 = GetAllUsersDesktopFolderPath();
            
            lbldesktop.Text = "当前桌面路径:" + desktopPath + "  |  " + dekpath5;
            string[] xx = Directory.GetDirectories(desktopPath);
            string[] path1 = Directory.GetFiles(desktopPath, "*.lnk");
            string[] path2 = Directory.GetFiles(dekpath5, "*.lnk");
            List<FilePathModel> lstAllFile = new List<FilePathModel>();
            if (path1 != null && path1.Length > 0)
            {
                foreach (var path in path1)
                {
                    FilePathModel temp = new FilePathModel();
 
                    temp.Name = Path.GetFileName(path);
                    temp.Filepath = path;
                    WshShell shell = new WshShell();
                    IWshShortcut lnkPath = (IWshShortcut)shell.CreateShortcut(path);
 
                    temp.FileTargetPath = lnkPath.TargetPath;
                    lstAllFile.Add(temp);
                }
            }
            if (path1 != null && path2.Length > 0)
            {
                foreach (var path in path2)
                {
                    FilePathModel temp = new FilePathModel();
 
                    temp.Name = Path.GetFileName(path);
                    temp.Filepath = path;
                    WshShell shell = new WshShell();
                    IWshShortcut lnkPath = (IWshShortcut)shell.CreateShortcut(path);
 
                    temp.FileTargetPath = lnkPath.TargetPath;
                    lstAllFile.Add(temp);
                }
            }
 
            if (txtSearch.Text.Trim() != "")
            {
                lstAllFile = lstAllFile.FindAll(r => r.Name.Contains(txtSearch.Text.Trim()));
            }
            c1FlexGrid1.SetDataBinding(lstAllFile, null, true);
        }
 
        [DllImport("shfolder.dll", CharSet = CharSet.Auto)]
        private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
        private const int MAX_PATH = 260;
        private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019;
        public static string GetAllUsersDesktopFolderPath()
        {
            StringBuilder sbPath = new StringBuilder(MAX_PATH);
            SHGetFolderPath(IntPtr.Zero, CSIDL_COMMON_DESKTOPDIRECTORY, IntPtr.Zero, 0, sbPath);
            return sbPath.ToString();
        }
        public class FilePathModel
        {
            public string Name { get; set; }
            public string Filepath { get; set; }
            public string FileTargetPath { get; set; }
        }
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值