我在未知文件夾(即c:\ dev)中有一個(.exe)快捷方式,指向我的應用程序。從ExecutableFile.exe獲取路徑快捷鍵
我一直試圖獲得快捷方式路徑,我的應用程序隨時啓動快捷方式。
我試過不同的方法,如Application.StartupPath,但它返回到應用程序可執行文件的路徑,而不是快捷方式的路徑。
我在未知文件夾(即c:\ dev)中有一個(.exe)快捷方式,指向我的應用程序。從ExecutableFile.exe獲取路徑快捷鍵
我一直試圖獲得快捷方式路徑,我的應用程序隨時啓動快捷方式。
我試過不同的方法,如Application.StartupPath,但它返回到應用程序可執行文件的路徑,而不是快捷方式的路徑。
此代碼將幫助您。 :)
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));
}
}
}
的問題,是我不知道鏈接路徑。它可以位於c:\ o c:\ dev或任何我需要的,所以我不能使用(const string path = @「C:\ link to foobar.lnk」;)。我將在系統中指向唯一一個.exe的不同鏈接,因此我需要鏈接的路徑,這些鏈接調用了.exe – kristonpelz
如果您需要更改從不同的快捷方式程序的流程然後從每個快捷方式傳遞參數,並在main(參數)閱讀。
如果你只需要得到該快捷方式的文件夾,然後確保使用
Environment.CurrentDirectory
我現在正在使用此rigth,但問題是如果客戶端移動.lnk進入o文件夾從參數路徑將是無效的。我正在尋找像傳遞鏈接的當前路徑作爲參數(即directory =%currentDir%)*,但不起作用的東西 – kristonpelz
這裏有一個解決方案,如果你不加入過時的要混亂圍繞「開始」文本是空白,並獲得該文件夾DLL對您的裝配:
private static string LnkToFile(string fileLink) {
List<string> saveout = new List<string>();
// KLUDGE Until M$ gets their $^%# together...
string[] vbs_script = {
"set WshShell = WScript.CreateObject(\"WScript.Shell\")\n",
"set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))\n",
"wscript.Echo Lnk.TargetPath\n"
};
string tempPath = System.IO.Path.GetTempPath();
string tempFile = System.IO.Path.Combine(tempPath, "pathenator.vbs");
File.WriteAllLines(tempFile, vbs_script);
var scriptProc = new System.Diagnostics.Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.Arguments = " //nologo \""
+ tempFile + "\" \"" + fileLink + "\"";
scriptProc.StartInfo.CreateNoWindow = true;
scriptProc.StartInfo.UseShellExecute = false;
scriptProc.StartInfo.RedirectStandardOutput = true;
scriptProc.Start();
scriptProc.WaitForExit();
string lineall
= scriptProc.StandardOutput.ReadToEnd().Trim('\r', '\n', ' ', '\t');
scriptProc.Close();
return lineall;
}
的可能重複[如何獲取應用程序的快捷方式的當前目錄路徑(http://stackoverflow.com/questions/15758941/how-to-get-the-current-directory -path-of-applications-shortcut) –
沒有任何方法可以找回.lnk文件你的過程。 –