private void toolStripButtonDelete_Click(object sender, EventArgs e) { if (MessageBox.Show(@"Are you sure?", @"Delete Fonts", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes) { return; } switch (tabControl1.SelectedIndex) { case 0: DeleteFromListView(lvSearch); break; case 1: foreach (var item in tvFonts.Nodes.Descendants().Where(n => n.Checked)) { MoveFont(item.Tag); tvFonts.Nodes.Remove(item); } break; case 2: DeleteFromListView(lvSelected); break; } _ctx.SaveChanges(); toolStripButtonDelete.Visible = false; }
private static void ProcessFiles(string folder) { // Get the list of files found in the directory. var dir = new DirectoryInfo(folder); var files = dir.GetFiles("*.?tf").ToList(); var fileCount = files.Count; using (var ctx = new FontInfosEntities()) { var FolderInDb = ctx.Folders.FirstOrDefault(f => f.Path == folder) ?? new Folder() { Path = folder }; if (FolderInDb.Fonts.Any()) { var fontsInDb = FolderInDb.Fonts; // remove from DB fonts not in folder var filesHashSet = new HashSet <string>(files.Select(x => x.FullName)); var ObsoleteFonts = fontsInDb.Where(f => !filesHashSet.Contains(f.FullName)).ToList(); if (ObsoleteFonts.Any()) { ctx.Fonts.RemoveRange(ObsoleteFonts); ctx.SaveChanges(); } // do not process fonts already in database var fontsHashSet = new HashSet <string>(fontsInDb.Select(f => f.FullName)); files.RemoveAll(f => fontsHashSet.Contains(f.FullName)); } if (!files.Any()) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine($"\n{folder} --- {fileCount}"); return; } var fonts = files.Select(f => new FontInfo(f, FolderInDb)); try { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"\n{folder} --- {fileCount} --- {fonts.Count()}"); ctx.Fonts.AddRange(fonts); ctx.SaveChanges(); } catch (Exception ex) { _errors.Add(folder); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine(ex.Message); Console.ReadLine(); } } }
static void Main(string[] args) { using (var ctx = new FontInfosEntities()) { Console.WriteLine("Selecting paths"); foreach (var folder in ctx.Folders) { Console.WriteLine(folder.Path); foreach (var font in folder.Fonts) { if (File.Exists(font.FullName)) { continue; } Console.WriteLine($"\t{font.FullName}"); ctx.Fonts.Remove(font); } } if (ctx.ChangeTracker.HasChanges()) { Console.WriteLine("Saving changes"); ctx.SaveChanges(); } } Console.WriteLine("Done."); Console.ReadLine(); }
private static void RemoveObsoleteFoldersFromDb(string folder) { using (var ctx = new FontInfosEntities()) { var foldersInDb = ctx.Folders.Where(f => f.Path.StartsWith(folder)).ToList(); if (!foldersInDb.Any()) { return; } // Remove empty folders and folders than no longer exist var obsoleteFolders = foldersInDb .Where(f => !f.Fonts.Any() || !Directory.Exists(f.Path)).ToList(); ctx.Folders.RemoveRange(obsoleteFolders); ctx.SaveChanges(); } }