加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何获取应用程序快捷方式的当前目录路径

发布时间:2020-12-15 08:36:24 所属栏目:百科 来源:网络整理
导读:我想获取当前目录路径,但不是应用程序位置,而是它的快捷方式位置. 我尝试了这些,但他们返回应用程序的位置. Directory.GetCurrentDirectory();Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);Path.GetDirectoryName(Sy
我想获取当前目录路径,但不是应用程序位置,而是它的快捷方式位置.

我尝试了这些,但他们返回应用程序的位置.

Directory.GetCurrentDirectory();
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);

解决方法

如果添加COM对象引用不是问题,请添加COM对象引用 – Windows脚本宿主对象模型

我在我的桌面文件夹中运行此代码,它确实有效.当前文件夹使用 – Environment.CurrentDirectory

using System;
using System.IO;
using IWshRuntimeLibrary;  //COM object -Windows Script Host Object Model

namespace csCon
{
    class Program
    {
        static void Main(string[] args)
        {
            // Folder is set to Desktop 
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var di = new DirectoryInfo(dir);
            FileInfo[] fis = di.GetFiles();
            if (fis.Length > 0)
            {
                foreach (FileInfo fi in fis)
                {
                    if (fi.FullName.EndsWith("lnk"))
                    {
                        IWshShell shell = new WshShell();
                        var lnk = shell.CreateShortcut(fi.FullName) as IWshShortcut;
                        if (lnk != null)
                        {
                            Console.WriteLine("Link name: {0}",lnk.FullName);
                            Console.WriteLine("link target: {0}",lnk.TargetPath);
                            Console.WriteLine("link working: {0}",lnk.WorkingDirectory);
                            Console.WriteLine("description: {0}",lnk.Description);
                        }

                    }
                }
            }
        }
    }
}

论坛代码参考:http://www.neowin.net/forum/topic/658928-c%23-resolve-lnk-files/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读