2016-12-29 14:15:45 +00:00
///
/// Copyright (c) 2016-2017 Sensus Metering Systems
///
using System ;
2016-10-18 11:15:15 +00:00
using System.IO ;
2016-10-11 14:18:54 +00:00
using System.Windows.Forms ;
2016-12-29 14:15:45 +00:00
using log4net ;
2016-10-11 14:18:54 +00:00
namespace DeviceTest
{
static class Program
{
2016-10-18 11:15:15 +00:00
/// Program information
public static string Version ; /// version string
public static DateTime BuildDateTime ; /// date and time of program build
2016-12-29 14:15:45 +00:00
/// log4net
static ILog log ;
const string log4netConfigFName = "log4netConfig.xml" ;
2016-10-18 11:15:15 +00:00
/// Local settings
public static readonly string LocAppDataDir ;
public static readonly string LocalSettingsFileName ;
public static readonly string LocalSettingsBackupName ;
public static LocalSettings LocalSettings ;
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
LocAppDataDir = Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) +
Path . DirectorySeparatorChar + "TestBenchFramework" + Path . DirectorySeparatorChar ;
LocalSettingsFileName = LocAppDataDir + "DeviceTest.xml" ;
LocalSettingsBackupName = LocAppDataDir + "DeviceTest.backup.xml" ;
}
/// <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 ( "Na tomto počítači nebol tento program ani raz spustený.\r\nPoužijú sa základné nastavenia." , "Upozornenie" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
Program . LocalSettings = new LocalSettings ( ) ;
Directory . CreateDirectory ( LocAppDataDir ) ;
Program . 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 ) ;
}
2016-12-29 14:15:45 +00:00
/// Configure and start logging
log4net . Config . XmlConfigurator . Configure ( new System . IO . FileInfo ( LocAppDataDir + log4netConfigFName ) ) ;
log = LogManager . GetLogger ( typeof ( Program ) ) ;
log . Fatal ( "--------------------------------------------------------------------------------" ) ;
log . Fatal ( string . Format ( "TBF ver.{0}" , Version ) ) ;
2016-10-18 11:15:15 +00:00
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
try
{
DeviceTestDlg dlg = new DeviceTestDlg ( ) ;
Application . Run ( dlg ) ;
}
catch ( Exception exc )
{
2016-12-29 14:15:45 +00:00
LogException ( log , "Exception in Application.Run(DeviceTestDlg)" , exc ) ;
2016-10-18 11:15:15 +00:00
MessageBox . Show ( "Program havaroval:" +
Environment . NewLine + Environment . NewLine + exc . Message +
( ( exc . InnerException = = null ) ? string . Empty : ( Environment . NewLine + exc . InnerException . Message ) ) +
Environment . NewLine + exc . StackTrace ,
"Pozor!" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
}
}
2016-12-29 14:15:45 +00:00
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 ( "--------------------------------------" ) ;
}
2016-10-18 11:15:15 +00:00
}
2016-10-11 14:18:54 +00:00
}