tbf/ResultsBrowser/Program.cs

158 lines
5.7 KiB
C#
Raw Permalink Normal View History

2015-12-12 20:44:36 +00:00
///
/// Copyright (c) 2015-2016 Sensus Metering Systems
2015-04-15 20:03:23 +00:00
///
2015-02-20 02:59:11 +00:00
using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using log4net;
using Config.Entities;
2015-02-20 02:59:11 +00:00
using ResultsBrowser.Forms;
using ResultsBrowser.Resources;
2015-02-20 02:59:11 +00:00
namespace ResultsBrowser
{
2015-12-12 20:44:36 +00:00
public class Program
{
static ILog log; /// log4net
/// Program information
public static string Version; /// version string
public static DateTime BuildDateTime; /// date and time of program build
2015-02-20 02:59:11 +00:00
/// Local settings
public static string ExecutableDirectory;
public static string ConfigDirectory;
public static string LogDirectory;
public static string LocalSettingsFileName;
public static string LocalSettingsBackupName;
public static LocalSettings LocalSettings;
2015-02-20 02:59:11 +00:00
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
/// Program version string
System.Reflection.Assembly thisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Version ver = thisAssembly.GetName().Version;
Version = string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build);
BuildDateTime = new System.IO.FileInfo(thisAssembly.Location).LastWriteTime;
/// Local program configuration directory including the trailing backslash
ExecutableDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
2021-05-13 15:12:00 +00:00
#if true
/// Config directory is a sibling directory of the executable directory
ConfigDirectory = Path.Combine(ExecutableDirectory, "..", string.Format("Cfg{0}", (args.Length >= 1) ? args[0] : string.Empty));
LogDirectory = Path.Combine(ExecutableDirectory, "..\\Log\\");
#else
/// Config directory is a directory in 'C:\Users\username\AppData\Local' folder
ConfigDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
string.Format("ResultsBrowser{0}", (args.Length >= 1) ? args[0] : string.Empty));
LogDirectory = "C:\\Temp\\ResultsBrowserLog\\";
#endif
LocalSettingsFileName = Path.Combine(ConfigDirectory, "config.xml");
LocalSettingsBackupName = Path.Combine(ConfigDirectory, "config.backup.xml");
/// Configue and start logging
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(ConfigDirectory + "log4netConfig.xml"));
log = LogManager.GetLogger(typeof(Program));
log.Fatal("------------------------------------------------------------------------------------------------");
log.Fatal("started");
///
/// Load the local settings
///
2015-12-12 20:44:36 +00:00
LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
if (LocalSettings == null)
{
/// Loading local seetings from regular config file failed. Use the backup
2015-12-12 20:44:36 +00:00
LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName);
if (LocalSettings == null)
{
MessageBox.Show(Strings.No_program_settings_found + Strings.Using_default_settings,
string.Empty,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
2015-12-12 20:44:36 +00:00
/// Create new default settings and save them
LocalSettings = new LocalSettings();
Directory.CreateDirectory(ConfigDirectory);
LocalSettings.Save();
}
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);
}
try
{
string culture = (LocalSettings.Language != null) ? LocalSettings.Language.Replace('_', '-') : "EN";
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
}
catch (Exception e)
{
string msg = e.Message;
MessageBox.Show(Strings.Selected_language_not_supported + Environment.NewLine + Strings.Using_English,
Strings.Warning,
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo("en");
}
2015-02-20 02:59:11 +00:00
2015-12-12 20:44:36 +00:00
/// This belongs to Application.Run(new MainWnd()) ...
/// and should be executed before opening 'loginDlg'
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2015-02-20 02:59:11 +00:00
try
{
/// Open the main application window
log.Info("Creating and opening the main window");
Application.Run(new ResultsBrowserWnd(args));
log.Info("The main window was closed");
}
catch (Exception e)
{
LogException(log, "Exception in Application.Run(new ResultsBrowserWnd(args))", e);
}
finally
{
/// Save local settings
LocalSettings.Save();
}
2015-02-20 02:59:11 +00:00
2015-12-12 20:44:36 +00:00
/// Close Fluent NHibernate
/// Todo
2015-02-20 02:59:11 +00:00
log.Info("Exiting application.");
2015-12-12 20:44:36 +00:00
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
LogException(log, "Unhandled exception", (Exception)args.ExceptionObject);
}
static void LogException(ILog log, string description, Exception e)
{
log.FatalFormat("---------------( {0} )---------------", description);
log.FatalFormat("Message : {0}", e.Message);
if (e.InnerException != null)
{
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
}
log.FatalFormat("StackTrace : {0}{1}", Environment.NewLine, e.StackTrace);
log.Fatal("--------------------------------------");
}
}
2015-02-20 02:59:11 +00:00
}