private static void CheckAndCreateFiles() { if (string.IsNullOrEmpty(FileSystem.CommandFile)) { FileSystem.CommandFile = FileSystem.DefaultCommandFile; FileSystem.AppendToFile(FileSystem.ConfigFile, "\nCommandFile=" + FileSystem.DefaultCommandFile); if (!FileSystem.FileExists(FileSystem.DefaultCommandFile)) { FileSystem.CreateFile(FileSystem.DefaultCommandFile); } } if (string.IsNullOrEmpty(FileSystem.ApplicationSettingsFile)) { FileSystem.ApplicationSettingsFile = FileSystem.DefaultApplicationSettingsFile; FileSystem.AppendToFile(FileSystem.ConfigFile, "\nApplicationSettingsFile=" + FileSystem.DefaultApplicationSettingsFile); if (!FileSystem.DirectoryExists(FileSystem.DefaultSettingsFolder)) { FileSystem.CreateDirectory(FileSystem.DefaultSettingsFolder); } } if (string.IsNullOrEmpty(FileSystem.UserSettingsFile)) { FileSystem.UserSettingsFile = FileSystem.DefaultUserSettingsFile; FileSystem.AppendToFile(FileSystem.ConfigFile, "\nUserSettingsFile=" + FileSystem.DefaultUserSettingsFile); if (!FileSystem.DirectoryExists(FileSystem.DefaultSettingsFolder)) { FileSystem.CreateDirectory(FileSystem.DefaultSettingsFolder); } } Settings.Initialize(); Log.WriteMessage("Loading user settings"); Settings.User.Load(); Log.WriteDebugMessage("User settings loaded"); Log.WriteDebugMessage("Attempting upgrade of application settings from old version of application (if needed)"); Settings.Application.Upgrade(); Log.WriteDebugMessage("Attempting upgrade of user settings from old version of application (if needed)"); Settings.User.Upgrade(); if (string.IsNullOrEmpty(FileSystem.ScreenshotsFile)) { ImageFormatCollection imageFormatCollection = new ImageFormatCollection(); ScreenCollection screenCollection = new ScreenCollection(); ScreenshotCollection screenshotCollection = new ScreenshotCollection(imageFormatCollection, screenCollection); screenshotCollection.SaveToXmlFile(0); } if (string.IsNullOrEmpty(FileSystem.EditorsFile)) { // Loading the editor collection will automatically create the default editors and add them to the collection. EditorCollection editorCollection = new EditorCollection(); editorCollection.LoadXmlFileAndAddEditors(); } if (string.IsNullOrEmpty(FileSystem.RegionsFile)) { RegionCollection regionCollection = new RegionCollection(); regionCollection.SaveToXmlFile(); } if (string.IsNullOrEmpty(FileSystem.ScreensFile)) { // Loading the screen collection will automatically create the available screens and add them to the collection. ScreenCollection screenCollection = new ScreenCollection(); screenCollection.LoadXmlFileAndAddScreens(new ImageFormatCollection()); } if (string.IsNullOrEmpty(FileSystem.TriggersFile)) { // Loading triggers will automatically create the default triggers and add them to the collection. TriggerCollection triggerCollection = new TriggerCollection(); triggerCollection.LoadXmlFileAndAddTriggers(); } if (string.IsNullOrEmpty(FileSystem.TagsFile)) { // Loading tags will automatically create the default tags and add them to the collection. TagCollection tagCollection = new TagCollection(); tagCollection.LoadXmlFileAndAddTags(); } if (string.IsNullOrEmpty(FileSystem.SchedulesFile)) { // Loading schedules will automatically create the default schedules and add them to the collection. ScheduleCollection scheduleCollection = new ScheduleCollection(); scheduleCollection.LoadXmlFileAndAddSchedules(); } }
/// <summary> /// Loads the configuration file. /// </summary> public static void Load() { try { if (!FileSystem.DirectoryExists(FileSystem.GetDirectoryName(FileSystem.ConfigFile))) { FileSystem.CreateDirectory(FileSystem.GetDirectoryName(FileSystem.ConfigFile)); } if (!FileSystem.FileExists(FileSystem.ConfigFile)) { string[] linesToWrite = { "# Auto Screen Capture Configuration File", "# Use this file to tell the application what folders and files it should utilize.", "# Each key-value pair can be the name of a folder or file or a path to a folder or file.", "# If only the folder name is given then it will be parsed as the sub-folder of the folder", "# where the executed autoscreen.exe binary is located.", "", "# This is the folder where screenshots will be stored by default.", "ScreenshotsFolder=" + FileSystem.DefaultScreenshotsFolder, "", "# If any errors are encountered then you will find them in this folder when DebugMode is enabled.", "DebugFolder=" + FileSystem.DefaultDebugFolder, "", "# Logs are stored in this folder when either Logging or DebugMode is enabled.", "LogsFolder=" + FileSystem.DefaultLogsFolder, "", "# This file is monitored by the application for commands issued from the command line while it's running.", "CommandFile=" + FileSystem.DefaultCommandFile, "", "# The application settings (such as DebugMode).", "ApplicationSettingsFile=" + FileSystem.DefaultApplicationSettingsFile, "", "# Your personal settings.", "UserSettingsFile=" + FileSystem.DefaultUserSettingsFile, "", "# References to image editors.", "EditorsFile=" + FileSystem.DefaultEditorsFile, "", "# References to regions.", "RegionsFile=" + FileSystem.DefaultRegionsFile, "", "# References to screens.", "ScreensFile=" + FileSystem.DefaultScreensFile, "", "# References to triggers.", "TriggersFile=" + FileSystem.DefaultTriggersFile, "", "# References to screenshots.", "ScreenshotsFile=" + FileSystem.DefaultScreenshotsFile, "", "# References to tags.", "TagsFile=" + FileSystem.DefaultTagsFile, "", "# References to schedules.", "SchedulesFile=" + FileSystem.DefaultSchedulesFile, "" }; FileSystem.WriteToFile(FileSystem.ConfigFile, linesToWrite); } foreach (string line in FileSystem.ReadFromFile(FileSystem.ConfigFile)) { if (string.IsNullOrEmpty(line) || line.StartsWith("#")) { continue; } string path; if (GetPath(line, REGEX_SCREENSHOTS_FOLDER, out path)) { FileSystem.ScreenshotsFolder = path; } if (GetPath(line, REGEX_DEBUG_FOLDER, out path)) { FileSystem.DebugFolder = path; } if (GetPath(line, REGEX_LOGS_FOLDER, out path)) { FileSystem.LogsFolder = path; } if (GetPath(line, REGEX_COMMAND_FILE, out path)) { FileSystem.CommandFile = path; } if (GetPath(line, REGEX_APPLICATION_SETTINGS_FILE, out path)) { FileSystem.ApplicationSettingsFile = path; } if (GetPath(line, REGEX_USER_SETTINGS_FILE, out path)) { FileSystem.UserSettingsFile = path; } if (GetPath(line, REGEX_EDITORS_FILE, out path)) { FileSystem.EditorsFile = path; } if (GetPath(line, REGEX_REGIONS_FILE, out path)) { FileSystem.RegionsFile = path; } if (GetPath(line, REGEX_SCREENS_FILE, out path)) { FileSystem.ScreensFile = path; } if (GetPath(line, REGEX_TRIGGERS_FILE, out path)) { FileSystem.TriggersFile = path; } if (GetPath(line, REGEX_SCREENSHOTS_FILE, out path)) { FileSystem.ScreenshotsFile = path; } if (GetPath(line, REGEX_TAGS_FILE, out path)) { FileSystem.TagsFile = path; } if (GetPath(line, REGEX_SCHEDULES_FILE, out path)) { FileSystem.SchedulesFile = path; } } CheckAndCreateFolders(); CheckAndCreateFiles(); } catch (Exception ex) { Log.WriteExceptionMessage("Config::Load", ex); } }
/// <summary> /// Writes a message (whether it be an error or just a general message) and the exception (if any). /// </summary> /// <param name="message">The message to write.</param> /// <param name="writeError">Determines if we write the message to the error file in the debug folder.</param> /// <param name="ex">The exception received from the .NET Framework.</param> public void Write(string message, bool writeError, Exception ex) { try { _mutexWriteFile.WaitOne(); string appVersion = "[(v" + _settings.ApplicationVersion + ") "; if (string.IsNullOrEmpty(_fileSystem.ErrorsFolder)) { _fileSystem.ErrorsFolder = AppDomain.CurrentDomain.BaseDirectory + @"!autoscreen" + _fileSystem.PathDelimiter + "errors" + _fileSystem.PathDelimiter; } if (string.IsNullOrEmpty(_fileSystem.LogsFolder)) { _fileSystem.LogsFolder = _fileSystem.LogsFolder + "logs" + _fileSystem.PathDelimiter; } if (!_fileSystem.DirectoryExists(_fileSystem.ErrorsFolder)) { _fileSystem.CreateDirectory(_fileSystem.ErrorsFolder); } if (!_fileSystem.DirectoryExists(_fileSystem.LogsFolder)) { _fileSystem.CreateDirectory(_fileSystem.LogsFolder); } // These are just general errors from the application so, if we have one, then write it out to the error file. if (writeError) { _fileSystem.AppendToFile(_fileSystem.ErrorsFolder + _fileSystem.ErrorFile, appVersion + DateTime.Now.ToString(_macroParser.DateFormat + " " + _macroParser.TimeFormat) + "] ERROR: " + message); } // Log any exception errors we encounter. if (ex != null) { string exceptionError = appVersion + DateTime.Now.ToString(_macroParser.DateFormat + " " + _macroParser.TimeFormat) + "] " + message + " - Exception Message: " + ex.Message + "\nInner Exception: " + (ex.InnerException != null ? ex.InnerException.Message : string.Empty) + "\nSource: " + ex.Source + "\nStack Trace: " + ex.StackTrace; _fileSystem.AppendToFile(_fileSystem.ErrorsFolder + _fileSystem.ErrorFile, exceptionError); // If we encounter an exception error it's probably better to just error out on exit // but we'll let the user decide if that's what they really want to do. if (_settings.Application == null || Convert.ToBoolean(_settings.Application.GetByKey("ExitOnError", _settings.DefaultSettings.ExitOnError).Value)) { Environment.Exit(1); } } else { // Write to the main log file. _fileSystem.AppendToFile(_fileSystem.LogsFolder + _fileSystem.LogFile + _extension, appVersion + DateTime.Now.ToString(_macroParser.DateFormat + " " + _macroParser.TimeFormat) + "] " + message); // Create a date-stamped directory if it does not already exist. if (!_fileSystem.DirectoryExists(_fileSystem.LogsFolder + DateTime.Now.ToString(_macroParser.DateFormat))) { _fileSystem.CreateDirectory(_fileSystem.LogsFolder + DateTime.Now.ToString(_macroParser.DateFormat)); } // Write to a log file within a directory representing the day when the message was logged. _fileSystem.AppendToFile(_fileSystem.LogsFolder + DateTime.Now.ToString(_macroParser.DateFormat) + _fileSystem.PathDelimiter + _fileSystem.LogFile + "_" + DateTime.Now.ToString(_macroParser.DateFormat) + ".txt", appVersion + DateTime.Now.ToString(_macroParser.DateFormat + " " + _macroParser.TimeFormat) + "] " + message); } } finally { _mutexWriteFile.ReleaseMutex(); } }