Ala*_*ett 5 c# windows shortcuts
回到.NET 1.0天,我写了一个方法来返回MS Windows上的快捷方式的目标.它通过使用Windows脚本托管对象模型的互操作和强制通过COM接口执行此操作:
private FileInfo GetFileFromShortcut(FileInfo shortcut)
{
FileInfo targetFile = null;
try
{
IWshRuntimeLibrary.WshShell wShell = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.WshShortcut wShortcut = (IWshRuntimeLibrary.WshShortcut)wShell.CreateShortcut(shortcut.FullName);
// if the file wasn't a shortcut then the TargetPath comes back empty
string targetName = wShortcut.TargetPath;
if (targetName.Length > 0)
{
targetFile = new FileInfo(targetName);
}
}
catch (Exception)
{ // will return a null targetFile if anything goes wrong
}
return targetFile;
}
Run Code Online (Sandbox Code Playgroud)
这仍然让我感到困扰,我希望用更优雅的东西取代它,但前提是替换实际上至少也是如此.我仍然找不到寻找快捷方式目标的本地C#方式.有没有,或者这仍然是做这种事情的最佳方式?
| 归档时间: |
|
| 查看次数: |
1629 次 |
| 最近记录: |