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

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 lstAllFile = new List();
            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; }
        }
    }
}

程序截图:

C#获取桌面快捷方式路径并读取其目标路径_第1张图片

 

源码下载地址:GetApplicationInstrallPath.zip  

 

你可能感兴趣的:(C#,.NET,使用技巧)