public static void Main(string[] args) { Defrag.IFileSystem fileSystem = new Defrag.Win32FileSystem("C:"); Defrag.Optimizer optimizer = new Defrag.Optimizer(fileSystem); Defrag.Win32ChangeWatcher watcher = new Defrag.Win32ChangeWatcher("C:\\"); System.IO.TextWriter log = new System.IO.StreamWriter(System.Console.OpenStandardOutput()); while (true) { String path = watcher.NextFile(); if (path != null) { try { optimizer.DefragFile(path, log); } catch (Win32Exception ex) { log.WriteLine(ex.Message); log.WriteLine("* * Failed! * *"); } finally { log.WriteLine(); log.Flush(); } } else { Thread.Sleep(500); } } }
private void MainLoop() { Defrag.IFileSystem fileSystem = new Defrag.Win32FileSystem("C:"); Defrag.Optimizer optimizer = new Defrag.Optimizer(fileSystem); Defrag.Win32ChangeWatcher watcher = new Defrag.Win32ChangeWatcher("C:\\"); // Create the source, if it does not already exist. EventLog.DeleteEventSource("Defrag Service"); if (!EventLog.SourceExists("Defrag Service")) { EventLog.CreateEventSource("Defrag Service", "Defrag Log"); } // Create an EventLog instance and assign its source. EventLog event_log = new EventLog(); event_log.Source = "Defrag Service"; event_log.Log = "Defrag Log"; event_log.WriteEntry("Hello"); while (m_running) { String path = watcher.NextFile(); if (path != null) { StringWriter log = new StringWriter(); EventLogEntryType type = EventLogEntryType.Error; try { optimizer.DefragFile(path, log); type = EventLogEntryType.Information; } catch (Exception ex) { log.WriteLine(ex.Message); log.WriteLine(ex.StackTrace); } finally { log.Flush(); event_log.WriteEntry(log.ToString(), type); } } else { Thread.Sleep(500); } } }