我想获取当前目录路径,但不是应用程序位置,而是它的快捷方式位置.
我尝试了这些,但他们返回应用程序的位置.
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/