예제 #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 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
                });
            }
        }