public void Clean(object objx)
        {
            VSCleanSetting obj = (VSCleanSetting)objx;

            FailList    = new List <string>();
            SuccessList = new List <string>();

            OrderedExclusions(obj);

            foreach (string path in obj.ScanPaths)
            {
                string f = GetFilenameAbsolute(path);
                DoClean(obj, f);
            }
            if (obj.RemoveEmptyDirectory)
            {
                RemoveEmptyDirectory(GetFilenameAbsolute(""));
            }

            IsRunning = false;
            if (Report != null)
            {
                Report(SuccessList, FailList);
            }
        }
Beispiel #2
0
        private void btnClean_Click(object sender, EventArgs e)
        {
            if (GetText(btnClean) == "Start")
            {
                VSCleanSetting obj = VSCleanLib.GetCurrentSetting();
                if (obj == null || obj.ExcludePaths == null)
                {
                    return;
                }

                if (obj.ScanPaths == null || obj.ScanPaths.Count == 0)
                {
                    MessageBoxShow("No path to start, add <project>\\bin or <project>\\obj to start");
                    return;
                }
                Stop();
                SetText(btnClean, "Stop");
                Lib        = new VSCleanLib();
                Lib.Report = new Action <List <string>, List <string> >(Report);
                ParameterizedThreadStart ts = new ParameterizedThreadStart(Lib.Clean);
                MainThread = new Thread(ts);
                MainThread.Start(obj);
                Lib.IsRunning = true;
            }
            else if (GetText(btnClean) == "Stop")
            {
                SetText(btnClean, "Start");
                Stop();
            }
        }
        /// <summary>
        /// Ordering ScanPaths/ExcludePaths by length without making the result ordered list having upper case
        /// </summary>
        /// <param name="obj"></param>
        private void OrderedExclusions(VSCleanSetting obj)
        {
            List <string> scanPaths    = obj.ScanPaths.Select(m => m.ToUpper()).ToList().OrderByDescending(m => m.Length).ToList();
            List <string> excludePaths = obj.ExcludePaths.Select(m => m.ToUpper()).ToList().OrderByDescending(m => m.Length).ToList();

            // Maintain Long
            List <string> resultScan    = new List <string>();
            List <string> resultExclude = new List <string>();

            foreach (string s in scanPaths)
            {
                foreach (string r in obj.ScanPaths)
                {
                    if (s.ToUpper() == r.ToUpper())
                    {
                        resultScan.Add(r);
                    }
                }
            }

            foreach (string s in excludePaths)
            {
                foreach (string r in obj.ExcludePaths)
                {
                    if (s.ToUpper() == r.ToUpper())
                    {
                        resultExclude.Add(r);
                    }
                }
            }

            obj.ScanPaths    = resultScan;
            obj.ExcludePaths = resultExclude;
        }
        public static bool AddScan(VSCleanSetting obj, string str)
        {
            if (obj == null)
            {
                obj = GetCurrentSetting();
            }
            string path = str;

            if (path == "")
            {
                path = GetWorkingDirectory();
            }
            if (Directory.Exists(path) == false)
            {
                return(false);
            }
            bool contains = ContainsScan(obj, path);

            if (contains)
            {
                return(false);
            }
            contains = ContainsExclude(obj, path);
            if (contains)
            {
                return(false);
            }

            obj.ScanPaths.Add(GetFilenameRelative(path));
            return(SaveCurrentSetting(obj));
        }
        public static bool ContainsExclude(VSCleanSetting obj, string file)
        {
            if (obj == null || obj.ExcludePaths == null)
            {
                return(false);
            }

            string file2 = file.TrimEnd(Path.DirectorySeparatorChar);

            if (file2 == Path.DirectorySeparatorChar.ToString())
            {
                file2 = GetFilenameAbsolute("").TrimEnd(Path.DirectorySeparatorChar);
            }
            if (Directory.Exists(file2))
            {
                file2 = file2 + Path.DirectorySeparatorChar;
            }

            foreach (string str in obj.ExcludePaths)
            {
                string str2 = GetFilenameAbsolute(str).TrimEnd(Path.DirectorySeparatorChar);
                if (Directory.Exists(str2))
                {
                    str2 = str2 + Path.DirectorySeparatorChar;
                }
                if (str2.ToLower() == file2.ToLower())
                {
                    return(true);
                }
            }
            return(false);
        }
 public static bool SaveCurrentSetting(VSCleanSetting obj)
 {
     if (obj == null || obj.ScanPaths == null)
     {
         return(false);
     }
     try
     {
         if (obj.DeleteCsVb)
         {
             if (obj.OnlyDeleteExtensions.Contains(".cs", StringComparer.OrdinalIgnoreCase))
             {
                 obj.OnlyDeleteExtensions.RemoveAll(n => n.Equals(".cs", StringComparison.OrdinalIgnoreCase));
             }
             if (obj.OnlyDeleteExtensions.Contains(".vb", StringComparer.OrdinalIgnoreCase))
             {
                 obj.OnlyDeleteExtensions.RemoveAll(n => n.Equals(".vb", StringComparison.OrdinalIgnoreCase));
             }
         }
         string str  = JsonConvert.SerializeObject(obj);
         string file = GetFilenameAbsolute(SettingFilename);
         File.WriteAllText(file, str);
         return(true);
     }
     catch { }
     return(false);
 }
        public static bool IsExcluded(VSCleanSetting obj, string file)
        {
            if (obj == null || obj.ExcludePaths == null)
            {
                return(false);
            }

            bool   isDirectory = false;
            string file2       = file.TrimEnd(Path.DirectorySeparatorChar);

            if (Directory.Exists(file2))
            {
                file2 = file2 + Path.DirectorySeparatorChar; isDirectory = true;
            }
            if (file2 == Path.DirectorySeparatorChar.ToString())
            {
                file2 = GetFilenameAbsolute("").TrimEnd(Path.DirectorySeparatorChar);
            }

            foreach (string str in obj.ExcludePaths)
            {
                string str2 = GetFilenameAbsolute(str).TrimEnd(Path.DirectorySeparatorChar);
                if (Directory.Exists(str2))
                {
                    str2 = str2 + Path.DirectorySeparatorChar;
                }
                if (str2.ToLower() == file2.ToLower())
                {
                    return(true);
                }
            }

            if (isDirectory == false && obj.OnlyDeleteExtensions != null && obj.OnlyDeleteExtensions.Count > 0)
            {
                //if ((file.ToLower().EndsWith(".cs") || file.ToLower().EndsWith(".vb")) && Path.GetFileName(file).ToLower().StartsWith("temporarygeneratedfile_"))
                //    return false;

                List <string> exts       = obj.OnlyDeleteExtensions;
                bool          mustDelete = false;
                for (int i = 0; i < exts.Count; i++)
                {
                    string ext = exts[i].ToLower();
                    if (file.ToLower().EndsWith(ext))
                    {
                        mustDelete = true;
                        break;
                    }
                }
                if (mustDelete)
                {
                    return(false);
                }
                return(true);
            }
            return(false);
        }
        public void ScanVS()
        {
            string abs = GetFilenameAbsolute("");

            ScanObj = GetCurrentSetting();
            ScanVSInner(abs);
            IsRunningScan = false;
            if (ScanCompleted != null)
            {
                ScanCompleted();
            }
        }
        private bool DefaultExclusion(VSCleanSetting obj, string file)
        {
            string file2 = file.TrimEnd(Path.DirectorySeparatorChar);

            foreach (string str in DefaultExclusions)
            {
                string str2 = GetFilenameAbsolute(str).TrimEnd(Path.DirectorySeparatorChar);
                if (str2.ToLower() == file2.ToLower())
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #10
0
        private void Include(ListView listView, string s)
        {
            if (s == CurrentDirectory)
            {
                s = "";
            }
            VSCleanSetting obj = VSCleanLib.GetCurrentSetting();

            if (VSCleanLib.AddScan(obj, VSCleanLib.GetFilenameAbsolute(s)))
            {
                VSCleanLib.RemoveExclude(obj, VSCleanLib.GetFilenameAbsolute(s));
                VSCleanLib.SaveCurrentSetting(obj);
            }
        }
        public static bool AddExclude(VSCleanSetting obj, string str)
        {
            if (obj == null)
            {
                obj = GetCurrentSetting();
            }
            string path     = str;
            bool   contains = ContainsExclude(obj, path);

            if (contains)
            {
                return(false);
            }

            obj.ExcludePaths.Add(GetFilenameRelative(path));
            return(SaveCurrentSetting(obj));
        }
 public static bool RemoveScan(VSCleanSetting obj, string file)
 {
     if (obj == null || obj.ScanPaths == null)
     {
         return(false);
     }
     for (int i = obj.ScanPaths.Count - 1; i >= 0; i--)
     {
         string str = obj.ScanPaths[i];
         str = GetFilenameAbsolute(str);
         if (str.ToLower() == file.ToLower())
         {
             obj.ScanPaths.RemoveAt(i);
             return(true);
         }
     }
     return(false);
 }
Beispiel #13
0
        public void ListViewDelete(ListView listView, bool isExclude)
        {
            if (listView.SelectedItems == null || listView.SelectedItems.Count == 0)
            {
                return;
            }
            VSCleanSetting obj = VSCleanLib.GetCurrentSetting();

            for (int i = listView.SelectedItems.Count - 1; i >= 0; i--)
            {
                string strx = listView.SelectedItems[i].Text;
                if (strx == CurrentDirectory)
                {
                    strx = "";
                }

                string str = VSCleanLib.GetFilenameAbsolute(strx);
                if (isExclude)
                {
                    if (VSCleanLib.ContainsExclude(obj, str))
                    {
                        VSCleanLib.RemoveExclude(obj, str);
                    }
                }
                else
                {
                    if (VSCleanLib.ContainsScan(obj, str))
                    {
                        VSCleanLib.RemoveScan(obj, str);
                    }
                }
            }
            if (VSCleanLib.SaveCurrentSetting(obj))
            {
                LoadSetting();
                return;
            }
            MessageBoxShow("Fail removing Path(s)");
            return;
        }
Beispiel #14
0
        private void LoadSetting()
        {
            if (InvokeRequired)
            {
                Invoke(new Action(LoadSetting), null);
                return;
            }
            listView1.Items.Clear();
            VSCleanSetting obj = VSCleanLib.GetCurrentSetting();

            if (obj == null || obj.ExcludePaths == null)
            {
                return;
            }
            foreach (string str in obj.ExcludePaths)
            {
                string f = str;
                if (string.IsNullOrEmpty(f))
                {
                    f = CurrentDirectory;
                }
                listView1.Items.Add(f);
            }

            listView2.Items.Clear();
            if (obj == null || obj.ScanPaths == null)
            {
                return;
            }
            foreach (string str in obj.ScanPaths)
            {
                string f = str;
                if (string.IsNullOrEmpty(f))
                {
                    f = CurrentDirectory;
                }
                listView2.Items.Add(f);
            }
        }
        public static bool ContainsScan(VSCleanSetting obj, string file)
        {
            if (obj == null || obj.ScanPaths == null)
            {
                return(false);
            }

            string file2 = file.TrimEnd(Path.DirectorySeparatorChar);

            if (file2 == "")
            {
                file2 = GetFilenameAbsolute("").TrimEnd(Path.DirectorySeparatorChar);
            }
            foreach (string str in obj.ScanPaths)
            {
                string str2 = GetFilenameAbsolute(str).TrimEnd(Path.DirectorySeparatorChar);
                if (str2.ToLower() == file2.ToLower())
                {
                    return(true);
                }
            }
            return(false);
        }
        public static VSCleanSetting GetCurrentSetting()
        {
            VSCleanSetting obj = null;

            try
            {
                string file = GetFilenameAbsolute(SettingFilename);
                if (File.Exists(file) == false)
                {
                    VSCleanSetting ep = new VSCleanSetting();
                    ep.RemoveEmptyDirectory = true;
                    ep.DeleteCsVb           = true;
                    ep.ExcludePaths         = new List <string>();
                    ep.ScanPaths            = new List <string>();
                    ep.OnlyDeleteExtensions = DefaultOnlyDeleteExtensions;
                    return(ep);
                }
                string str = File.ReadAllText(file);
                obj = JsonConvert.DeserializeObject <VSCleanSetting>(str);

                if (obj.DeleteCsVb)
                {
                    if (obj.OnlyDeleteExtensions.Contains(".cs", StringComparer.OrdinalIgnoreCase) == false)
                    {
                        obj.OnlyDeleteExtensions.Add(".cs");
                    }
                    if (obj.OnlyDeleteExtensions.Contains(".vb", StringComparer.OrdinalIgnoreCase) == false)
                    {
                        obj.OnlyDeleteExtensions.Add(".vb");
                    }
                }
            }
            catch
            {
            }
            return(obj);
        }
        public static bool AddCurrentScan()
        {
            VSCleanSetting obj = GetCurrentSetting();

            return(AddScan(obj, ""));
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            VSCleanSetting obj = VSCleanLib.GetCurrentSetting();

            if (string.IsNullOrEmpty(txtSubPath.Text))
            {
                return;
            }
            if (obj == null)
            {
                MessageBox.Show("Error reading " + VSCleanLib.SettingFilename);
                return;
            }
            string path      = txtSubPath.Text;
            string checkPath = path.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;

            if (checkPath.StartsWith(VSCleanLib.GetWorkingDirectory(), StringComparison.CurrentCultureIgnoreCase) == false)
            {
                MessageBox.Show("Path must be Sub Path from VSClean.exe");
                return;
            }
            if (File.Exists(VSCleanLib.GetFilenameAbsolute(path)) == false && Directory.Exists(VSCleanLib.GetFilenameAbsolute(path)))
            {
                path = path.TrimEnd(new char[] { '\\' }) + "\\";
            }
            if (Exclude == false && Directory.Exists(VSCleanLib.GetFilenameAbsolute(path)) == false)
            {
                MessageBox.Show("Path must be Directory");
                return;
            }

            bool contains = false;

            if (Exclude)
            {
                contains = VSCleanLib.ContainsExclude(obj, path);
            }
            else
            {
                contains = VSCleanLib.ContainsScan(obj, path);
            }
            if (contains)
            {
                MessageBox.Show("Path/File already exist in list");
                return;
            }
            if (Exclude)
            {
                obj.ExcludePaths.Add(VSCleanLib.GetFilenameRelative(path));
            }
            else
            {
                obj.ScanPaths.Add(VSCleanLib.GetFilenameRelative(path));
            }

            if (VSCleanLib.SaveCurrentSetting(obj))
            {
                Close();
                return;
            }
            MessageBox.Show("Fail adding Path to " + VSCleanLib.SettingFilename);
            return;
        }
        private void DoClean(VSCleanSetting obj, string str)
        {
            str = GetFilenameAbsolute(str);
            if (DefaultExclusion(obj, str))
            {
                return;
            }
            bool exist = false;

            try
            {
                exist = Directory.Exists(str);
            }
            catch { }
            if (exist)
            {
                if (IsExcluded(obj, str))
                {
                    return;
                }
                string[] files = null;
                try
                {
                    files = Directory.GetFiles(str);
                }
                catch { }
                if (files != null && files.Length > 0)
                {
                    foreach (string file in files)
                    {
                        if (IsExcluded(obj, file))
                        {
                            continue;
                        }
                        if (DefaultExclusion(obj, file))
                        {
                            continue;
                        }

                        string ss = GetFilenameRelative(file);
                        try
                        {
                            File.Delete(file);
                            if (string.IsNullOrEmpty(ss) == false && SuccessList.Contains(ss) == false)
                            {
                                SuccessList.Add(ss);
                            }
                        }
                        catch
                        {
                            if (string.IsNullOrEmpty(ss) == false && FailList.Contains(ss) == false)
                            {
                                FailList.Add(ss);
                            }
                        }
                    }
                }
                string[] dirs = null;
                try
                {
                    dirs = Directory.GetDirectories(str);
                }
                catch { }
                if (dirs != null && dirs.Length > 0)
                {
                    foreach (string dir in dirs)
                    {
                        DoClean(obj, dir);
                    }
                    try
                    {
                        dirs = Directory.GetDirectories(str);
                    }
                    catch { }
                }
                if (dirs == null || dirs.Length == 0)
                {
                    try
                    {
                        Directory.Delete(str);
                    }
                    catch
                    {
                    }
                }
            }
            exist = false;
            try
            {
                exist = File.Exists(str);
            }
            catch { }
            if (exist)
            {
                string ss = GetFilenameRelative(str);
                if (IsExcluded(obj, str))
                {
                    return;
                }
                try
                {
                    File.Delete(str);
                    if (string.IsNullOrEmpty(ss) == false && SuccessList.Contains(ss) == false)
                    {
                        SuccessList.Add(ss);
                    }
                }
                catch
                {
                    if (string.IsNullOrEmpty(ss) == false && FailList.Contains(ss) == false)
                    {
                        FailList.Add(ss);
                    }
                }
            }
        }