public static void Main(string[] args) { bool tracedExceptionThrown = false; try { // define the data paths string subPath = @"CodeReflection\Razor"; // safely use a hosting engine configured with an additional common data path and additional local user data path using(SnapInHostingEngine host = new SnapInHostingEngine(subPath, subPath)) { try { // run the hosting engine using the command line and the currently executing assembly (aka. the exe for the process) host.Run(args, System.Reflection.Assembly.GetExecutingAssembly()); } catch(System.Exception systemException) { // flag the fact that we are going to trace this exception and rethrow it tracedExceptionThrown = true; // give the loggers a chance to catch it if they have been successfully loaded before the // host is disposed of and the logging sub system detached from the Trace and Debug output System.Diagnostics.Trace.WriteLine(systemException); // rethrow the exception so that it may be displayed for the user throw systemException; } } } catch(System.Exception systemException) { // if the exception hasn't already been traced if (!tracedExceptionThrown) { tracedExceptionThrown = true; // trace it now System.Diagnostics.Trace.WriteLine(systemException); } // also, since it's more likely we'll not have a debugger attached, display the exception to the user string info = string.Format("The following exception was thrown by '{0}'.\n\nPlease refer to the log files for further information.\n\n{1}", Application.ProductName, systemException.ToString()); System.Windows.Forms.MessageBox.Show(null, info, "Application Exception"); // exit the current thread to force safe application shutdown Application.ExitThread(); } finally { // one final trace to let everyone know we have shutdown completely System.Diagnostics.Trace.WriteLine("'" + Application.ProductName + "' has " + (tracedExceptionThrown ? "terminated because of an exception." : "exited gracefully.")); } }
/// <summary> /// Initializes a new instance of the SnapInHostingEngine class /// </summary> /// <param name="additionalCommonAppDataPath">Additional path in which data common to all users will be stored. Example: "Company Name\Application Name\Version X.X.X"</param> /// <param name="additionalLocalUserAppDataPath">Additional path in which data particular to the current user will be stored. Example: "Company Name\Application Name\Version X.X.X"</param> public SnapInHostingEngine(string additionalCommonAppDataPath, string additionalLocalUserAppDataPath) { // thunk to this instance, the one and only _theInstance = this; // create a new application context _applicationContext = new SnapInApplicationContext(); // create a new instance manager _instanceManager = new ApplicationInstanceManager(); // combine the common app data path and the additional path to form this instance's common app data path _commonDataPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData), additionalCommonAppDataPath); // combine the local user app data path and the additional path to form this instance's local user app data path _localUserDataPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), additionalLocalUserAppDataPath); // combine the local user path with logs to form the logs path _logsDataPath = Path.Combine(_localUserDataPath, "Logs"); // combine the log path with the file name to form the full log path _logFilename = Path.Combine(_logsDataPath, "Log.txt"); // format the common configuration filename _commonConfigurationFilename = Path.Combine(_commonDataPath, SnapInHostingEngine.DefaultCommonConfigurationFilename); // format the local user configuration filename _localUserConfigurationFilename = Path.Combine(_localUserDataPath, SnapInHostingEngine.DefaultLocalUserConfigurationFilename); // format the configuration engine's configuration filename _configurationEngineConfigurationFilename = Path.Combine(_commonDataPath, ConfigurationEngine.DefaultConfigurationFilename); // format the installation engine's configuration filename _installationEngineConfigurationFilename = Path.Combine(_localUserDataPath, InstallationEngine.DefaultConfigurationFilename); // wire up to our own snapin events so that we can catch the events first and proxy some work before anyone else gets the events this.SnapInInstalled += new SnapInDescriptorEventHandler(OnInternalSnapInInstalled); this.SnapInUninstalled += new SnapInDescriptorEventHandler(OnInternalSnapInUninstalled); // create new window manager _windowManger = new WindowManager(); // create a new xml configuration manager _xmlConfigurationManager = new XmlConfigurationManager(); _xmlConfigurationManager.EnumeratingConfigurations += new XmlConfigurationManagerEventHandler(OnConfigurationManagerEnumeratingConfigurations); // create a new menu item security manager _menuItemSecurityManager = new MenuItemSecurityManager(); }