示例#1
0
        private static Win32Program LnkProgram(string path)
        {
            try
            {
                var    program = CreateWin32Program(path);
                string target  = ShellLinkHelper.RetrieveTargetPath(path);

                if (!string.IsNullOrEmpty(target))
                {
                    if (!(File.Exists(target) || Directory.Exists(target)))
                    {
                        // If the link points nowhere, consider it invalid.
                        return(InvalidProgram);
                    }

                    program.LnkResolvedPath           = program.FullPath;
                    program.LnkResolvedExecutableName = Path.GetFileName(target);

                    // Using CurrentCulture since this is user facing
                    program.FullPath = Path.GetFullPath(target).ToLowerInvariant();

                    program.Arguments = ShellLinkHelper.Arguments;

                    // A .lnk could be a (Chrome) PWA, set correct AppType
                    program.AppType = program.IsWebApplication()
                        ? ApplicationType.WebApplication
                        : GetAppTypeFromPath(target);

                    var description = ShellLinkHelper.Description;
                    if (!string.IsNullOrEmpty(description))
                    {
                        program.Description = description;
                    }
                    else
                    {
                        var info = FileVersionInfoWrapper.GetVersionInfo(target);
                        if (!string.IsNullOrEmpty(info?.FileDescription))
                        {
                            program.Description = info.FileDescription;
                        }
                    }
                }

                return(program);
            }
            catch (System.IO.FileLoadException e)
            {
                ProgramLogger.Warn($"Couldn't load the link file at {path}. This might be caused by a new link being created and locked by the OS.", e, MethodBase.GetCurrentMethod().DeclaringType, path);
                return(InvalidProgram);
            }

            // Only do a catch all in production. This is so make developer aware of any unhandled exception and add the exception handling in.
            // Error caused likely due to trying to get the description of the program
            catch (Exception e)
            {
                ProgramLogger.Exception($"|An unexpected error occurred in the calling method LnkProgram at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);

                return(InvalidProgram);
            }
        }
示例#2
0
        private static Win32 LnkProgram(string path)
        {
            var program = Win32Program(path);

            try
            {
                const int       MAX_PATH = 260;
                StringBuilder   buffer   = new StringBuilder(MAX_PATH);
                ShellLinkHelper _helper  = new ShellLinkHelper();
                string          target   = _helper.retrieveTargetPath(path);

                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);
                        program.hasArguments    = _helper.hasArguments;
                        program.Arguments       = _helper.Arguments;

                        var description = _helper.description;
                        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
        }
示例#3
0
        private static Win32Program LnkProgram(string path)
        {
            try
            {
                var           program  = CreateWin32Program(path);
                const int     MAX_PATH = 260;
                StringBuilder buffer   = new StringBuilder(MAX_PATH);

                string target = ShellLinkHelper.RetrieveTargetPath(path);

                if (!string.IsNullOrEmpty(target))
                {
                    if (File.Exists(target) || Directory.Exists(target))
                    {
                        program.LnkResolvedPath = program.FullPath;

                        // Using InvariantCulture since this is user facing
                        program.FullPath = Path.GetFullPath(target).ToLowerInvariant();
                        program.AppType  = GetAppTypeFromPath(target);

                        var description = ShellLinkHelper.Description;
                        if (!string.IsNullOrEmpty(description))
                        {
                            program.Description = description;
                        }
                        else
                        {
                            var info = FileVersionInfoWrapper.GetVersionInfo(target);
                            if (!string.IsNullOrEmpty(info?.FileDescription))
                            {
                                program.Description = info.FileDescription;
                            }
                        }
                    }
                }

                return(program);
            }

            // Only do a catch all in production. This is so make developer aware of any unhandled exception and add the exception handling in.
            // Error caused likely due to trying to get the description of the program
            catch (Exception e)
            {
                ProgramLogger.Exception($"|An unexpected error occurred in the calling method LnkProgram at {path}", e, MethodBase.GetCurrentMethod().DeclaringType, path);

                return(new Win32Program()
                {
                    Valid = false, Enabled = false
                });
            }
        }