Beispiel #1
0
        private static void Proc_DataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                if (e.Data.StartsWith("Program:"))
                {
                    string file = e.Data.Replace("Program:", "").TrimStart();

                    if (Path.IsPathRooted(file) && !Path.GetDirectoryName(file).StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.Windows)))
                    {
                        if (!FileExistance.FileExists(file))
                        {
                            try
                            {
                                if (!BackedUp)
                                {
                                    string datetime = DateTime.Now.ToString("Mdhms");

                                    RunCommand("netsh", "advfirewall export " + "\"" + AppDomain.CurrentDomain.BaseDirectory + "\\Backup\\" + datetime + "_FirwallPolicy.wfw" + "\"");

                                    BackedUp = true;
                                }

                                EasyLogger.Info("Removing (inbound/outbound) " + file + " from Windows Firewall...");
                                RunCommand("netsh", "advfirewall firewall delete rule name=all program= " + "\"" + file + "\"");
                            }
                            catch (Exception ex)
                            {
                                EasyLogger.Error(ex.Message);
                            }
                        }
                    }
                }
            }
        }
        private static void RemoveBrokenShortcuts(string shortcut)
        {
            try
            {
                Shell shell = new Shell();

                string shortcut_path = shortcut.Substring(0, shortcut.LastIndexOf("\\"));
                string shortcut_name = shortcut.Substring(shortcut.LastIndexOf("\\") + 1);

                if (shortcut_name.EndsWith(".lnk"))
                {
                    Folder shortcut_folder = shell.NameSpace(shortcut_path);

                    FolderItem folder_item = shortcut_folder.Items().Item(shortcut_name);

                    try
                    {
                        ShellLinkObject lnk  = (ShellLinkObject)folder_item.GetLink;
                        string          path = lnk.Path;
                        if (!string.IsNullOrEmpty(path))
                        {
                            try
                            {
                                if (!FileExistance.FileExists(path))
                                {
                                    EasyLogger.Info("Deleting broken shortcut: " + shortcut + ". The target doesn't exits: " + path);
                                    try
                                    {
                                        File.Delete(shortcut);
                                    }
                                    catch { /* ignore */ }
                                    try
                                    {
                                        Directory.Delete(shortcut);
                                    }
                                    catch { /* ignore */ }
                                }
                            }
                            catch { /* ignore */ }
                        }
                    }
                    catch { /* ignore */ }
                }

                // Epic Games & Steam
                if (shortcut_name.EndsWith(".url"))
                {
                    string line;
                    bool   deleteItem = false;

                    StreamReader file = new StreamReader(shortcut);
                    while ((line = file.ReadLine()) != null)
                    {
                        if (line.Contains("IconFile="))
                        {
                            string shortPath = line.Replace("IconFile=", "");

                            if (!FileExistance.FileExists(shortPath))
                            {
                                EasyLogger.Info("Deleting broken shortcut: " + shortcut + ". The target icon file doesn't exits: " + shortPath);

                                try
                                {
                                    deleteItem = true;
                                    break;
                                }
                                catch (Exception ex)
                                {
#if DEBUG
                                    EasyLogger.Error(ex);
#endif
                                    break;
                                }
                            }
                        }
                    }

                    file.Close();

                    if (deleteItem)
                    {
                        File.Delete(shortcut);
                    }
                }
            }
            catch (Exception ex)
            {
                EasyLogger.Error(ex);
            }
        }