De vuelta en .NET 1.0 días Escribí un método para devolver el objetivo de un acceso directo en MS Windows. Lo hizo mediante el uso de una interoperabilidad a la secuencia de comandos Alojamiento Windows Object Model y bruta fuerzan a través de la interfaz COM:¿Hay alguna manera de seguir un atajo del sistema de archivos de Windows programáticamente en C# sin usar 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;
}
errores Esta siendo yo, y yo estaba buscando para reemplazar esto con algo más elegante, pero sólo si el reemplazo realmente funciona al menos tan bien. Todavía no puedo encontrar una forma nativa de C# de encontrar el objetivo de un atajo. ¿Hay alguno, o sigue siendo la mejor manera de hacer este tipo de cosas?
Gran pregunta. No he pensado en cómo hacerlo funcionar sin interoperabilidad COM – Sung
No pude evitar usar el shell a través del código no administrado para lograr esto desafortunadamente. – RobS