|
private const
string REG_PATH = @"HKEY_CURRENT_USER\Software\NavWin\Defrag";
static void
Main(string[] args)
{
// Only defrag once a day, use the registry to
store this
string lastRunDate = ReadSetting("LastRunDate", "");
string today = DateTime.Now.ToString("dd-MM-yyyy");
if (lastRunDate.ToString() == today)
{
// the defrag already ran once today, do not do it
again
return;
}
// The very first time the app runs, this reg key
will return
double maxWait = Double.Parse(ReadSetting("MaxWaitMinutes", "5"));
System.Threading.Thread.Sleep(Convert.ToInt32(1000.0 * 60.0 * maxWait));
string writeResultPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
writeResultPath = Path.Combine(writeResultPath,
"NavWin");
if (!Directory.Exists(writeResultPath))
Directory.CreateDirectory(writeResultPath);
writeResultPath = Path.Combine(writeResultPath,
"Defrag");
if (!Directory.Exists(writeResultPath))
Directory.CreateDirectory(writeResultPath);
writeResultPath = Path.Combine(writeResultPath,
"navwin_defrag_output.txt");
writeResultPath = ReadSetting("ResultsPath",
writeResultPath);
// Expected format is space delimited c: d:
string defragDrives = ReadSetting("DefragDrives", "c:");
char[] sep = {' '};
string[] drives = defragDrives.Split(sep);
bool appendOutput = false;
foreach (string
drive in drives)
{
if (drives.Length > 0)
{
Defrag(drive, writeResultPath, appendOutput);
appendOutput = true;
}
}
Registry.SetValue(REG_PATH, "LastRunDate", today);
// write the registry for max wait as a convenience
Registry.SetValue(REG_PATH, "MaxWaitMinutes", maxWait.ToString());
Registry.SetValue(REG_PATH, "ResultsPath", writeResultPath);
Registry.SetValue(REG_PATH, "DefragDrives", defragDrives);
}
|