161 lines
6.8 KiB
C#
161 lines
6.8 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Common;
|
|
using Common.Forms;
|
|
using SharedDatabase.Entities;
|
|
using WorkflowConfigurator.Resources;
|
|
|
|
namespace WorkflowConfigurator
|
|
{
|
|
static class Program
|
|
{
|
|
/// Constants
|
|
public static GID[] SettingsAccessLevel = new GID[] { GID.TraceabilityManagement }; /// Group membership to access settings
|
|
public const string ConfigFName = "config.xml";
|
|
public const string BackupConfigFName = "config.backup.xml";
|
|
public const string Log4NetConfigFName = "log4netConfig.xml";
|
|
|
|
/// Program information
|
|
public static readonly string Version; /// version string
|
|
public static readonly DateTime BuildDateTime; /// date and time of program build
|
|
public static readonly string ExeDirectory;
|
|
public static readonly string ConfigDirectory;
|
|
public static readonly string LogDirectory;
|
|
public static readonly string LocalSettingsFileName;
|
|
public static readonly string LocalSettingsBackupName;
|
|
|
|
public static MainWnd MainWnd;
|
|
|
|
/// Local settings
|
|
public static LocalSettings LocalSettings;
|
|
|
|
static Program()
|
|
{
|
|
/// Program version string
|
|
Assembly thisAssembly = Assembly.GetExecutingAssembly();
|
|
Version ver = thisAssembly.GetName().Version;
|
|
Version = string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build);
|
|
BuildDateTime = new FileInfo(thisAssembly.Location).LastWriteTime;
|
|
|
|
/// Local program configuration directory including the trailing backslash
|
|
ExeDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
ConfigDirectory = Path.Combine(ExeDirectory, "..", "Cfg");
|
|
LogDirectory = Path.Combine(ExeDirectory, "..", "Logs");
|
|
LocalSettingsFileName = Path.Combine(ConfigDirectory, ConfigFName);
|
|
LocalSettingsBackupName = Path.Combine(ConfigDirectory, BackupConfigFName);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// The main entry point for the application.
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
///
|
|
/// Load the local settings
|
|
///
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
if (Program.LocalSettings == null)
|
|
{
|
|
/// Loading local seetings from regular config file failed. Use the backup
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName);
|
|
if (Program.LocalSettings == null)
|
|
{
|
|
MessageBox.Show(string.Format("{0}{1}{2}", Strings.Program_is_running_for_the_1st_time_on_this_PC,
|
|
Environment.NewLine,
|
|
Strings.Default_settings_are_used),
|
|
Strings.Warning,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Exclamation);
|
|
Program.LocalSettings = new LocalSettings(true); /// Create new default local setting
|
|
Directory.CreateDirectory(ConfigDirectory);
|
|
Program.LocalSettings.Save();
|
|
|
|
if (File.Exists(Path.Combine(ExeDirectory, "SampleConfig", Log4NetConfigFName)))
|
|
{
|
|
File.Copy(Path.Combine(ExeDirectory, "SampleConfig", Log4NetConfigFName),
|
|
Path.Combine(ConfigDirectory, Log4NetConfigFName));
|
|
}
|
|
|
|
File.SetAttributes(Path.Combine(ConfigDirectory, Log4NetConfigFName), FileAttributes.Normal);
|
|
}
|
|
else
|
|
{
|
|
LocalSettings.Save(); /// Save the settings to overwrite the wrong file
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/// Loading local seetings from the regular config file was successful. Update the backup
|
|
File.Copy(Program.LocalSettingsFileName, Program.LocalSettingsBackupName, true);
|
|
}
|
|
|
|
///
|
|
/// Configure and start logging
|
|
///
|
|
log4net.Config.XmlConfigurator.Configure(new FileInfo(Path.Combine(ConfigDirectory, Log4NetConfigFName)));
|
|
ILog log = LogManager.GetLogger(typeof(Program));
|
|
log.Fatal("------------------------------------------------------------------------------------------------");
|
|
log.FatalFormat("WorkfowConfigurator v.{0} started", Version);
|
|
|
|
///
|
|
/// Set language
|
|
///
|
|
try
|
|
{
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Program.LocalSettings.Language);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageBox.Show("Selected language is not supported.\nGoing to use English.",
|
|
"Warning",
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Asterisk);
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
|
|
}
|
|
|
|
///
|
|
/// Run the WinForm application
|
|
///
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
try
|
|
{
|
|
MainWnd = new MainWnd();
|
|
Application.Run(MainWnd);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
if (exc is QuitAppException)
|
|
{
|
|
ModelessForm.CloseForm();
|
|
log.FatalFormat("Program was not running: {0}", exc.Message);
|
|
MessageBox.Show(exc.Message,
|
|
Strings.Warning,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Exclamation);
|
|
}
|
|
else
|
|
{
|
|
log.FatalFormat("Program crashed:{0}{1}Stack trace:{0}{2}", Environment.NewLine, exc.Message, exc.StackTrace);
|
|
|
|
MessageBox.Show("Program crashed:" +
|
|
Environment.NewLine + Environment.NewLine + exc.Message +
|
|
((exc.InnerException == null) ? string.Empty : (Environment.NewLine + exc.InnerException.Message)) +
|
|
Environment.NewLine + exc.StackTrace,
|
|
Strings.Warning,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|