Пример #1
0
        protected AppInfo GetAppInfo(string path, bool onlyPrograms, Dictionary <string, object> uniq)
        {
            //Add files
            if (File.Exists(path))
            {
                string ext       = System.IO.Path.GetExtension(path).ToLower();
                string fullPath  = String.Empty;
                string appPath   = String.Empty;
                string appArgs   = String.Empty;
                string imagePath = String.Empty;

                if (ext == ".lnk")
                {
                    var shortcut = LnkHelper.OpenLnk(path);

                    appPath = shortcut.TargetPath;
                    appArgs = shortcut.Arguments;
                    var icoLocation = shortcut.IconLocation;

                    if (appArgs == null)
                    {
                        appArgs = String.Empty;
                    }

                    if (appPath.Contains("{"))
                    {
                        appPath = MsiShortcutParser.ParseShortcut(path);
                    }
                    else if (!icoLocation.Contains("{"))
                    {
                        imagePath = icoLocation;
                    }

                    if (!String.IsNullOrEmpty(appArgs))
                    {
                        fullPath = "\"" + appPath + "\"" + " " + appArgs;
                    }
                    else
                    {
                        fullPath = appPath;
                    }

                    if (!String.IsNullOrEmpty(imagePath))
                    {
                        var imageLocation = imagePath.Split(',');
                        if (imageLocation.Length > 0)
                        {
                            imagePath = imageLocation[0].Trim();
                        }
                        else
                        {
                            imagePath = String.Empty;
                        }
                    }
                }
                else
                {
                    appPath = fullPath = path;
                }

                if (String.IsNullOrEmpty(appPath))
                {
                    return(null);
                }
                //continue;

                if (onlyPrograms && !FilterApps(appPath, appArgs))
                {
                    return(null);
                }
                //continue;

                if (!String.IsNullOrEmpty(fullPath))
                {
                    if (!uniq.ContainsKey(fullPath.ToLower()))
                    {
                        string appName = Path.GetFileNameWithoutExtension(path);
                        uniq.Add(fullPath.ToLower(), null);
                        return(_WorkItem.AppData.CreateNewAppInfo(null, appName, fullPath, imagePath));
                        //result.Add(_WorkItem.AppData.CreateNewAppInfo(null, appName, fullPath, imagePath));
                    }
                }
            }
            else if (!onlyPrograms)            // Add folders
            {
                if (Directory.Exists(path))
                {
                    if (!uniq.ContainsKey(path.ToLower()))
                    {
                        uniq.Add(path.ToLower(), null);
                        return(_WorkItem.AppData.CreateNewAppInfo(null, path));
                        //result.Add(_WorkItem.AppData.CreateNewAppInfo(null, path));
                    }
                }
            }

            return(null);
        }
Пример #2
0
        public void Add(string shortcutPath, string basePath = null)
        {
            //Open Shortcut File
            WshShell    WinShell = new WshShell();
            WshShortcut WinShortcut;
            Shortcut    lShortcut;


            //REFACTOR: Method LoadFromFile in Shortcut
            WinShortcut = WinShell.CreateShortcut(shortcutPath);
            string relativePath     = "";
            string shortcutFileName = "";

            // Check if is a MSI Package

            string shortcutTargetPath = WinShortcut.TargetPath;
            string sShortcut          = null;

            if (WinShortcut.TargetPath.IndexOf("windows\\installer", 0, WinShortcut.TargetPath.Length, StringComparison.InvariantCultureIgnoreCase) > 0)
            {
                //Test MSI Shortcut
                sShortcut = MsiShortcutParser.ParseShortcut(shortcutPath);
            }

            if (sShortcut != null)
            {
                shortcutTargetPath = sShortcut;
            }

            foreach (Shortcut LocalShortcut in this)
            {
                //TODO: Add other criteria (e.g. Arguments)
                if (LocalShortcut.TargetPath == shortcutTargetPath)
                {
                    return;
                }
            }

            //Relative Path
            if (basePath != null)
            {
                relativePath = shortcutPath.Replace(basePath, "");
            }

            shortcutFileName = Path.GetFileName(shortcutPath);
            relativePath     = Path.GetDirectoryName(relativePath);

            lShortcut = new Shortcut
            {
                DisplayName      = Path.GetFileNameWithoutExtension(shortcutPath),
                TargetPath       = shortcutTargetPath,
                WorkingDirectory = WinShortcut.WorkingDirectory,
                Arguments        = WinShortcut.Arguments,
                Hotkey           = WinShortcut.Hotkey,
                IconLocation     = WinShortcut.IconLocation,
                ObjectID         = Guid.NewGuid(),
                MenuPath         = relativePath,
                WindowStyle      = WinShortcut.WindowStyle,
                PutOnStartMenu   = true
            };
            lShortcut.FindIcon(shortcutPath);
            this.Add(lShortcut);
        }