C# Tips-ショートカットからフルパス、アイコンの取得
ショートカットからその実行パスとアイコンの取得です。
ショートカットとは下記です。
これらを取得するには少し癖があります。
ソースコード
namespace Sample.FileCheck { public static class ShortCut { public static void Getter() { List<ShortCutFile> scfList = new List<ShortCutFile>(); string[] files = Directory.GetFiles(@"./FileCheck/temp/", "*"); foreach (string file in files) { //拡張子取得 string extension = Path.GetExtension(file); //ショートカット if (extension.Equals(".lnk")) { //Windows Script Host Object Model参照 ShortCutFile scf = new ShortCutFile(); scf.FileName = Path.GetFileName(file); WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshShortcut sc = (IWshShortcut)shell.CreateShortcut(file); // ショートカットのリンク先の取得 scf.ActualPath = string.Format("{0}/{1}", sc.WorkingDirectory, Path.GetFileName(scf.ActualPath)); //アイコン取得 System.Drawing参照 scf.Image = Icon.ExtractAssociatedIcon(scf.ActualPath).ToBitmap(); //アイコン保存 scf.Image.Save(string.Format("Img-{0}.jpg", scf.FileName), ImageFormat.Jpeg); scfList.Add(scf); } } foreach (ShortCutFile scf in scfList) { //実行 Process.Start(scf.ActualPath); } } } public class ShortCutFile { public string FileName { get; set; } public string ActualPath { get; set; } public Image Image { get; set; } } }
まとめ
実際のパスは"TargetPath"に入っているかと思いましたが、、、
"WorkingDirectory"にパスが入っているみたいです。