private static Win32 LnkProgram(string path) { var program = Win32Program(path); try { var link = new ShellLink(); const uint STGM_READ = 0; ((IPersistFile)link).Load(path, STGM_READ); var hwnd = new _RemotableHandle(); link.Resolve(ref hwnd, 0); const int MAX_DESCRIPTION = 150; StringBuilder bufferDescription = new StringBuilder(MAX_DESCRIPTION); String description = String.Empty; try { link.GetDescription(bufferDescription, MAX_DESCRIPTION); } catch (COMException e) { Log.Error($"|Win32.LnkProgram|cannot get description <{path}>. HResult: <{e.HResult}>. Exception: <{e}>."); bufferDescription.Clear(); } description = bufferDescription.ToString(); if (!string.IsNullOrEmpty(description)) { program.Description = description; } else { const int MAX_PATH = 260; StringBuilder bufferPath = new StringBuilder(MAX_PATH); var data = new _WIN32_FIND_DATAW(); const uint SLGP_SHORTPATH = 1; link.GetPath(bufferPath, bufferPath.Capacity, ref data, SLGP_SHORTPATH); var target = bufferPath.ToString(); if (!string.IsNullOrEmpty(target)) { var extension = Extension(target); if (extension == ExeExtension && File.Exists(target)) { var info = FileVersionInfo.GetVersionInfo(target); if (!string.IsNullOrEmpty(info.FileDescription)) { program.Description = info.FileDescription; } } } } return(program); } catch (Exception e) { Log.Exception($"|Win32.LnkProgram|Exception when parsing shortcut <{path}>", e); program.Valid = false; return(program); } }
private static Win32 LnkProgram(string path) { var program = Win32Program(path); try { var link = new ShellLink(); const uint STGM_READ = 0; ((IPersistFile)link).Load(path, STGM_READ); var hwnd = new _RemotableHandle(); link.Resolve(ref hwnd, 0); const int MAX_PATH = 260; StringBuilder buffer = new StringBuilder(MAX_PATH); var data = new _WIN32_FIND_DATAW(); const uint SLGP_SHORTPATH = 1; link.GetPath(buffer, buffer.Capacity, ref data, SLGP_SHORTPATH); var target = buffer.ToString(); if (!string.IsNullOrEmpty(target)) { var extension = Extension(target); if (extension == ExeExtension && File.Exists(target)) { buffer = new StringBuilder(MAX_PATH); link.GetDescription(buffer, MAX_PATH); var description = buffer.ToString(); if (!string.IsNullOrEmpty(description)) { program.Description = description; } else { var info = FileVersionInfo.GetVersionInfo(target); if (!string.IsNullOrEmpty(info.FileDescription)) { program.Description = info.FileDescription; } } } } return(program); } catch (COMException e) { // C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception Log.Error($"COMException when parsing shortcut: {path}, HResult: {e.HResult}"); Log.Exception(e); program.Valid = false; return(program); } catch (Exception e) { Log.Error($"Error when parsing shortcut: {path}"); Log.Exception(e); program.Valid = false; return(program); } }
private static Win32 LnkProgram(string path) { var program = Win32Program(path); try { var link = new ShellLink(); const uint STGM_READ = 0; ((IPersistFile)link).Load(path, STGM_READ); var hwnd = new _RemotableHandle(); link.Resolve(ref hwnd, 0); const int MAX_PATH = 260; StringBuilder buffer = new StringBuilder(MAX_PATH); var data = new _WIN32_FIND_DATAW(); const uint SLGP_SHORTPATH = 1; link.GetPath(buffer, buffer.Capacity, ref data, SLGP_SHORTPATH); var target = buffer.ToString(); var extension = Extension(target); if (!string.IsNullOrEmpty(target) && (extension == ExeExtension)) { buffer = new StringBuilder(MAX_PATH); link.GetDescription(buffer, MAX_PATH); var description = buffer.ToString(); if (!string.IsNullOrEmpty(description)) { program.Description = description; } else { var info = FileVersionInfo.GetVersionInfo(target); if (!string.IsNullOrEmpty(info.FileDescription)) { program.Description = info.FileDescription; } } } return(program); } catch (Exception) { Log.Error($"Error when parsing shortcut: {path}"); return(program); } }
private static Win32 LnkProgram(string path) { var program = Win32Program(path); try { var link = new ShellLink(); const uint STGM_READ = 0; ((IPersistFile)link).Load(path, STGM_READ); var hwnd = new _RemotableHandle(); link.Resolve(ref hwnd, 0); const int MAX_PATH = 260; StringBuilder buffer = new StringBuilder(MAX_PATH); var data = new _WIN32_FIND_DATAW(); const uint SLGP_SHORTPATH = 1; link.GetPath(buffer, buffer.Capacity, ref data, SLGP_SHORTPATH); var target = buffer.ToString(); if (!string.IsNullOrEmpty(target)) { var extension = Extension(target); if (extension == ExeExtension && File.Exists(target)) { program.LnkResolvedPath = program.FullPath; program.FullPath = Path.GetFullPath(target).ToLower(); program.ExecutableName = Path.GetFileName(target); buffer = new StringBuilder(MAX_PATH); link.GetDescription(buffer, MAX_PATH); var description = buffer.ToString(); if (!string.IsNullOrEmpty(description)) { program.Description = description; } else { var info = FileVersionInfo.GetVersionInfo(target); if (!string.IsNullOrEmpty(info.FileDescription)) { program.Description = info.FileDescription; } } } } return(program); } catch (COMException e) { // C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception ProgramLogger.LogException($"|Win32|LnkProgram|{path}" + "|Error caused likely due to trying to get the description of the program", e); program.Valid = false; return(program); } #if !DEBUG //Only do a catch all in production. This is so make developer aware of any unhandled exception and add the exception handling in. catch (Exception e) { ProgramLogger.LogException($"|Win32|LnkProgram|{path}" + "|An unexpected error occurred in the calling method LnkProgram", e); program.Valid = false; return(program); } #endif }
private static Win32 LnkProgram(string path) { var program = Win32Program(path); try { var link = new ShellLink(); const uint STGM_READ = 0; ((IPersistFile) link).Load(path, STGM_READ); var hwnd = new _RemotableHandle(); link.Resolve(ref hwnd, 0); const int MAX_PATH = 260; StringBuilder buffer = new StringBuilder(MAX_PATH); var data = new _WIN32_FIND_DATAW(); const uint SLGP_SHORTPATH = 1; link.GetPath(buffer, buffer.Capacity, ref data, SLGP_SHORTPATH); var target = buffer.ToString(); if (!string.IsNullOrEmpty(target)) { var extension = Extension(target); if (extension == ExeExtension && File.Exists(target)) { buffer = new StringBuilder(MAX_PATH); link.GetDescription(buffer, MAX_PATH); var description = buffer.ToString(); if (!string.IsNullOrEmpty(description)) { program.Description = description; } else { var info = FileVersionInfo.GetVersionInfo(target); if (!string.IsNullOrEmpty(info.FileDescription)) { program.Description = info.FileDescription; } } } } return program; } catch (COMException e) { // C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception Log.Error($"COMException when parsing shortcut: {path}, HResult: {e.HResult}"); Log.Exception(e); program.Valid = false; return program; } catch (Exception e) { Log.Error($"Error when parsing shortcut: {path}"); Log.Exception(e); program.Valid = false; return program; } }
public static string getLinkLocation(string path ) { using (ShellLink link = new ShellLink()) { link.Load(path); return link.GetPath(); } }
private static Win32 LnkProgram(string path) { var program = Win32Program(path); try { var link = new ShellLink(); const uint STGM_READ = 0; ((IPersistFile)link).Load(path, STGM_READ); var hwnd = new _RemotableHandle(); link.Resolve(ref hwnd, 0); const int MAX_PATH = 260; StringBuilder buffer = new StringBuilder(MAX_PATH); var data = new _WIN32_FIND_DATAW(); const uint SLGP_SHORTPATH = 1; link.GetPath(buffer, buffer.Capacity, ref data, SLGP_SHORTPATH); var target = buffer.ToString(); var extension = Extension(target); if (!string.IsNullOrEmpty(target) && (extension == ExeExtension)) { buffer = new StringBuilder(MAX_PATH); link.GetDescription(buffer, MAX_PATH); var description = buffer.ToString(); if (!string.IsNullOrEmpty(description)) { program.Description = description; } else { var info = FileVersionInfo.GetVersionInfo(target); if (!string.IsNullOrEmpty(info.FileDescription)) { program.Description = info.FileDescription; } } } return program; } catch (Exception) { Log.Error($"Error when parsing shortcut: {path}"); return program; } }