【问题标题】:How to get the current directory path of application's shortcut如何获取应用程序快捷方式的当前目录路径
【发布时间】:2013-03-23 10:36:47
【问题描述】:

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

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

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

【问题讨论】:

  • shortcut location 是指指向实际 *.exe 的实际 *.lnk 文件?
  • 我不知道它的扩展名是否为 .ink 但是的,它是一个指向实际 .exe 的文件
  • 在windows机器上,扩展名是*.lnk
  • @psubsee2003 我不知道我去了快捷方式属性,但它没有显示扩展名:D
  • 您似乎认为应用程序只有一个且完全是一个快捷方式。这通常不正确。

标签: c# console-application


【解决方案1】:

如果添加 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/

【讨论】:

    【解决方案2】:

    根据 MSDN 中的进程 API 参考,给定进程的进程 STARTUPINFO 结构包含有关标题成员中的快捷方式 .lnk 文件的信息。在这种情况下,dwFlags 结构成员中存在一个标志 - 所以它似乎并不总是设置(我猜你是否直接运行 exe)

    来自 MSDN:

    STARTF_TITLEISLINKNAME:0x00000800

    lpTitle 成员包含用户调用的快捷方式文件 (.lnk) 的路径 开始这个过程。当 .lnk 文件指向 启动的应用程序被调用。大多数应用程序不需要设置此值。 此标志不能与 STARTF_TITLEISAPPID 一起使用。

    Reference here.

    【讨论】:

      【解决方案3】:

      试试这个:

      Environment.CurrentDirectory
      

      来自MSDN

      获取或设置当前工作目录的完全限定路径。

      【讨论】:

      • 这不会返回应用程序路径而不是快捷方式路径吗?
      • stackoverflow.com/questions/1343406/… 这个链接从试图获取程序集目录的相反角度进一步解释了它
      • 根据他在 OP 代码中使用它的位置,当前目录可能不是快捷目录位置。他必须在他的程序开始执行后立即调用它,并且希望没有其他程序更改它,因为Environment.CurrentDirectory 是一个环境变量,所有应用程序都可以更改
      【解决方案4】:

      我认为您需要使用 COM 并添加对“Microsoft Shell 控制和自动化”的引用,如this 博客文章中所述:

      这是一个使用那里提供的代码的示例:

      namespace Shortcut
      {
          using System;
          using System.Diagnostics;
          using System.IO;
          using Shell32;
      
          class Program
          {
              public static string GetShortcutTargetFile(string shortcutFilename)
              {
                  string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
                  string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
      
                  Shell shell = new Shell();
                  Folder folder = shell.NameSpace(pathOnly);
                  FolderItem folderItem = folder.ParseName(filenameOnly);
                  if (folderItem != null)
                  {
                      Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                      return link.Path;
                  }
      
                  return string.Empty;
              }
      
              static void Main(string[] args)
              {
                  const string path = @"C:\link to foobar.lnk";
                  Console.WriteLine(GetShortcutTargetFile(path));
              }
          }
      }
      

      【讨论】:

      • 我想你不明白我的问题...假设我有一个 exe 并在一个文件夹中创建了它的快捷方式...当我运行快捷方式时,我希望它显示快捷方式
      【解决方案5】:

      使用 System.Reflection;

       string currentAssemblyDirectoryName = Path.GetDirectoryName(
                                              Assembly.GetExecutingAssembly()
                                                      .Location
                                              );
      

      也适用于您可以使用的网络应用程序:

      网络应用程序:

      Request.PhysicalApplicationPath
      

      http://msdn.microsoft.com/en-us/library/system.web.httprequest.physicalapplicationpath.aspx

      获取应用程序路径:)

      【讨论】:

      • 这将获取程序集的目录路径,而不是包含启动应用程序的快捷方式的目录路径。
      【解决方案6】:

      由于创建快捷方式是工作流程的一部分,只需将快捷方式的工作目录设置为“%cd%”,然后在应用程序中使用:

      Environment.CurrentDirectory
      

      显然,您希望在应用调用的任何代码更改它之前捕获该值。

      使用 Windows 资源管理器创建快捷方式时,您没有设置工作目录的选项。因此,在创建它之后,通过右键单击它并选择属性打开它的属性页,然后将 Start in 字段设置为%cd%。创建此类快捷方式后,您可以将其移动或复制到您希望应用运行的文件夹中。

      【讨论】:

        【解决方案7】:

        如果您不想按照其他答案中的建议使用 COM 对象

        您可以将文件作为常规文本文件打开,在 \x000 字符上拆分,然后检查生成的字符串数组。其中之一显然是链接目标:"C:\path\to\file" 之类的东西,或者在 UNC 的情况下为"\\computers\path\to\file"

        string lnkFilePath  "...";
        
        var file = System.IO.File.ReadAllText(lnkFilePath);
        var contents = Regex.Split(file, "[\x00]+");
        var paths = contents.Where(line => Regex.IsMatch(line, @"^([A-Z]:\\|^\\\\)\S+.*?"));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多