如何获取应用程序的快捷方式的当前目录路径 [英] How to get the current directory path of application's shortcut
问题描述
我想,但不是应用程序的位置,但它的快捷方式位置的当前目录路径。
我尝试了这些,但他们返回应用程序的位置。
Directory.GetCurrentDirectory();
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()基本代码);
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()位置。);
Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
如果添加COM对象引用是没有问题的,添加COM对象参考 - Windows脚本宿主对象模型
我在桌面文件夹中运行此代码,它做的工作。对于当前文件夹用 - Environment.CurrentDirectory
使用系统;
:使用System.IO;
使用IWshRuntimeLibrary; // COM对象-Windows脚本宿主对象模型
命名空间csCon
{
类节目
{
静态无效的主要(字串[] args)
{
//文件夹设置为桌面
串DIR = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
变种DI =新DirectoryInfo的(DIR);
的FileInfo [] = FIS di.GetFiles();
如果(fis.Length大于0)
{
的foreach(FileInfo的网络连接FIS)
{
如果(fi.FullName.EndsWith(LNK) )
{
IWshShell壳=新的WshShell();
VAR LNK = shell.CreateShortcut(fi.FullName)为IWshShortcut;
如果(LNK!= NULL)
{
Console.WriteLine(链接名称:{0},lnk.FullName);
Console.WriteLine(链接目标:{0},lnk.TargetPath);
Console.WriteLine(环节的工作:{0},lnk.WorkingDirectory);
Console.WriteLine(说明:{0},lnk.Description);
}
}
}
}
}
}
}
从论坛代码参考:的 http://www.neowin.net/forum/topic/658928-c%23-resolve-lnk-files/
I want to get the current directory path but not of the application location but of it's shortcut location.
I tried these but they return the application's location.
Directory.GetCurrentDirectory();
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
If adding a COM Object reference is not a problem , Add COM Object Reference - Windows Script Host Object Model
i ran this code in my desktop folder and it did work. for current folder use - 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);
}
}
}
}
}
}
}
Code Reference from Forum : http://www.neowin.net/forum/topic/658928-c%23-resolve-lnk-files/
这篇关于如何获取应用程序的快捷方式的当前目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!