public static ShortcutItem ConvertFilePathToShortcut(string file)
        {
            //If the dropped item is a file

            string       strFilename   = string.Empty;
            string       strTargetPath = file;
            ShortcutItem item          = new ShortcutItem()
            {
                Id = Guid.NewGuid().ToString()
            };

            if (System.IO.File.Exists(file))
            {
                strFilename = System.IO.Path.GetFileNameWithoutExtension(file);
                string strIconPath  = string.Empty;
                string strExtension = System.IO.Path.GetExtension(file);



                IconExtractor extractor = null;
                Icon          ico       = null;
                string        iconPath  = null;
                int           iconIndex = 0;

                bool hasIcon = false;
                bool iconAlreadyExtracted = false;
                if (".lnk" == strExtension.ToLower())
                {
                    //get extensions
                    string       linkPathName = @file;
                    WshShell     shell        = new WshShell();
                    IWshShortcut link         = (IWshShortcut)shell.CreateShortcut(linkPathName);

                    if (!System.IO.Directory.Exists(link.TargetPath))
                    {
                        //Ensure that this is not a folder
                        if (System.IO.Path.GetExtension(link.TargetPath) == ".exe")
                        {
                            //has icon
                            if (!string.IsNullOrEmpty(link.IconLocation))
                            {
                                hasIcon = true;

                                //get icon details
                                string[] aryIconPart = link.IconLocation.Split(',');

                                //if the application part is missing/empty, get it straight from the EXE
                                if (string.IsNullOrEmpty(aryIconPart[0]))
                                {
                                    aryIconPart[0] = link.TargetPath;
                                    iconPath       = System.Environment.ExpandEnvironmentVariables(aryIconPart[0]);
                                    iconIndex      = int.Parse(aryIconPart[1]);
                                }
                                else
                                {
                                    iconAlreadyExtracted = (System.IO.Path.GetExtension(aryIconPart[0]) == ".ico");

                                    //aryIconPart[0] = link.TargetPath;
                                    //iconPath = System.Environment.ExpandEnvironmentVariables(aryIconPart[0]);
                                    iconPath  = aryIconPart[0];
                                    iconIndex = int.Parse(aryIconPart[1]);
                                }
                            }
                        }

                        strTargetPath = link.TargetPath;
                    }
                }
                else if (".exe" == strExtension.ToLower())
                {
                    //exe
                    hasIcon       = true;
                    iconPath      = file;
                    iconIndex     = 0;
                    strTargetPath = file;
                }
                else
                {
                    //file
                    hasIcon       = false;
                    iconPath      = null;
                    iconIndex     = 0;
                    strTargetPath = file;
                }


                if (hasIcon)
                {
                    if (iconAlreadyExtracted)
                    {
                        item.IconPath = iconPath;
                    }
                    else
                    {
                        //extract icon
                        try
                        {
                            //Extract the icon
                            extractor = new IconExtractor(iconPath);
                            ico       = extractor.GetIcon(iconIndex);

                            //strIconPath =  AppConfig.GetIcon(strFilename) + ".ico";
                            strIconPath = CommonUtil.CreateUniqueFilename(AppConfig.GetIconMapFile(strFilename) + ".ico");

                            System.IO.FileStream fs = new System.IO.FileStream(strIconPath, System.IO.FileMode.OpenOrCreate);
                            ico.Save(fs);
                            fs.Close();
                            fs.Dispose();

                            item.IconPath = System.IO.Path.GetFileName(strIconPath);
                        }
                        catch (Exception)
                        {
                            item.IconPath = "";
                        }
                        finally
                        {
                            if (extractor != null)
                            {
                                extractor.Dispose();
                                extractor = null;
                            }

                            if (ico != null)
                            {
                                ico.Dispose();
                                ico = null;
                            }
                        }
                    }
                }
            }
            else if (System.IO.Directory.Exists(file))
            {
                //directory
                DirectoryInfo inf = new DirectoryInfo(file);
                strFilename = inf.Name;
            }

            item.Text        = strFilename;
            item.Application = strTargetPath;

            return(item);
        }
示例#2
0
        /// <summary>
        /// <para>Created By    : YUKUANG</para>
        /// <para>Created Date  : 15 Oct 2009</para>
        /// <para>Modified By   : -</para>
        /// <para>Modified Date : -</para>
        /// <para>---------------------------------------------------------------</para>
        /// <para></para>
        /// <para>Changes</para>
        /// <para>---------------------------------------------------------------</para>
        /// <para></para>
        /// <para>Description</para>
        /// <para>---------------------------------------------------------------</para>
        /// For rebuilding of icons for older release of the Shortcut Manager.
        /// It will extract icons and patch up the shortcut files
        /// </summary>
        private void RebuildIcons()
        {
            WriteLine("Getting Group Name Config");

            DeleteUnmappedIcons();

            List <string> groupNames = ShortcutUtil.GetShortcutGroupNames();

            foreach (string groupName in groupNames)
            {
                List <ShortcutItem> shortcuts = ShortcutUtil.GetShortcuts(groupName);
                foreach (ShortcutItem item in shortcuts)
                {
                    string applicationPath = item.Application;
                    if (applicationPath.EndsWith(".exe") && System.IO.File.Exists(applicationPath))
                    {
                        //Try to extract the icon file
                        try
                        {
                            Write(string.Format("Extracting Icon: {0}", applicationPath));
                            IconExtractor extractor = new IconExtractor(applicationPath);

                            //Gets the first icon in the Executable
                            System.Drawing.Icon ico = extractor.GetIcon(0);

                            //create unique name
                            string uniqueFilename = System.IO.Path.GetFileNameWithoutExtension(applicationPath);
                            string iconPath       = System.IO.Path.Combine(AppConfig.IconFolder, uniqueFilename + ".ico");
                            string uniqueIconPath = MakeUniqueFilename(iconPath);

                            bool isNonUnique = !uniqueIconPath.Equals(iconPath);

                            //Save the Icon using a FileStream
                            using (System.IO.FileStream fs = new System.IO.FileStream(uniqueIconPath, System.IO.FileMode.OpenOrCreate))
                            {
                                ico.Save(fs);
                                fs.Close();
                                fs.Dispose();
                            }
                            WriteLine("\tExtracted\n");



                            if (isNonUnique)
                            {
                                //non-unique file path detected. check if it's same bytes
                                WriteLine("Non unique icon found. Checking hashcode.");

                                byte[] b1 = System.IO.File.ReadAllBytes(uniqueIconPath);
                                byte[] b2 = System.IO.File.ReadAllBytes(iconPath);

                                int s1 = System.Convert.ToBase64String(b1).GetHashCode();
                                int s2 = System.Convert.ToBase64String(b2).GetHashCode();

                                WriteLine(string.Format("  {0} vs {1}", s1, s2));
                                if (s1 == s2)
                                {
                                    WriteLine("Deleting duplicate.");
                                    System.IO.File.Delete(uniqueIconPath);
                                    uniqueIconPath = iconPath;
                                }
                            }

                            //Patch the datarow
                            item.IconPath = System.IO.Path.GetFileName(uniqueIconPath);
                        }
                        catch (Exception)
                        {
                            //display error
                            MessageBox.Show(
                                "Unable to extract icon for [" + groupName + "/" + item.Text + "]",
                                "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                ShortcutUtil.UpdateGroupShortcut(groupName, shortcuts.ToArray());
            }
        }