微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我想获取当前目录路径,但不是应用程序位置,而是它的快捷方式位置.

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

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/

原文地址:https://www.jb51.cc/csharp/244189.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐