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/

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...