175 lines
5.7 KiB
C#
175 lines
5.7 KiB
C#
///
|
|
/// Copyright (c) 2015-2016 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using ResultsBrowser.Forms;
|
|
using ResultsBrowser.Resources;
|
|
|
|
namespace ResultsBrowser
|
|
{
|
|
public class Program
|
|
{
|
|
/// Program information
|
|
public static string Version; /// version string
|
|
public static DateTime BuildDateTime; /// date and time of program build
|
|
|
|
/// Local settings
|
|
public static readonly string ExecutableDirectory;
|
|
public static readonly string ConfigDirectory;
|
|
public static readonly string LogDirectory;
|
|
public static readonly string LocalSettingsFileName;
|
|
public static readonly string LocalSettingsBackupName;
|
|
public static LocalSettings LocalSettings;
|
|
|
|
|
|
public static System.Globalization.CultureInfo AltCulture;
|
|
|
|
static readonly ILog log; /// log4net
|
|
|
|
/// <summary>
|
|
/// Main window object.
|
|
/// </summary>
|
|
public static MainWnd MainWnd;
|
|
public static RBrowserWnd RBrowserWnd;
|
|
public static ResultsBrowserWnd ResultsBrowserWnd;
|
|
|
|
static Program()
|
|
{
|
|
/// 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);
|
|
ConfigDirectory = Path.Combine(ExecutableDirectory, "..\\Cfg\\");
|
|
LogDirectory = Path.Combine(ExecutableDirectory, "..\\Log\\");
|
|
LocalSettingsFileName = ConfigDirectory + "config.xml";
|
|
LocalSettingsBackupName = 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");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// The main entry point for the application.
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main(string[] args)
|
|
{
|
|
///
|
|
/// Load the local settings
|
|
///
|
|
LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
if (LocalSettings == null)
|
|
{
|
|
/// Loading local seetings from regular config file failed. Use the backup
|
|
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);
|
|
|
|
/// 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.Replace('_', '-');
|
|
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");
|
|
}
|
|
|
|
//AltCulture = null;
|
|
AltCulture = new System.Globalization.CultureInfo("SK");
|
|
|
|
|
|
/// This belongs to Application.Run(new MainWnd()) ...
|
|
/// and should be executed before opening 'loginDlg'
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
|
try
|
|
{
|
|
/// Open the main application window
|
|
log.Info("Creating the main window");
|
|
#if false //IPERLST || IPERLST_SPECIAL
|
|
MainWnd = new MainWnd();
|
|
log.Info("Opening the main window");
|
|
Application.Run(MainWnd);
|
|
#else
|
|
ResultsBrowserWnd = new ResultsBrowserWnd(args);
|
|
log.Info("Opening the main window");
|
|
Application.Run(ResultsBrowserWnd);
|
|
#endif
|
|
log.Info("The main window was closed");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogException(log, "Exception in Application.Run(MainWnd)", e);
|
|
}
|
|
finally
|
|
{
|
|
/// Save local settings
|
|
LocalSettings.Save();
|
|
}
|
|
|
|
/// Close Fluent NHibernate
|
|
/// Todo
|
|
|
|
log.Info("Exiting application.");
|
|
}
|
|
|
|
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("--------------------------------------");
|
|
}
|
|
}
|
|
} |