Ejemplo n.º 1
0
        //[ChoSingletonInstanceInitializer]
        public void Initialize()
        {
            if (_initialized)
            {
                return;
            }

            lock (_padLock)
            {
                if (_initialized)
                {
                    return;
                }

                _initialized = true;

                try
                {
                    EntryAssemblyLocation = ChoAssembly.GetEntryAssembly().Location;
                    EntryAssemblyFileName = System.IO.Path.GetFileName(EntryAssemblyLocation);
                }
                catch (System.Security.SecurityException ex)
                {
                    // This security exception will occur if the caller does not have
                    // some undefined set of SecurityPermission flags.
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }

                try
                {
                    AppEnvironment = ConfigurationManager.AppSettings["appEnvironment"];
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.ToString());
                }

                try
                {
                    SharedEnvironmentConfigFilePath = ConfigurationManager.AppSettings["sharedEnvironmentConfigFilePath"];
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.ToString());
                }

                try
                {
                    if (ConfigurationManager.AppSettings["appConfigPath"].IsNullOrWhiteSpace())
                    {
                        ApplicationConfigFilePath = System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                    }
                    else
                    {
                        ApplicationConfigFilePath = ConfigurationManager.AppSettings["appConfigPath"].Trim();
                    }
                }
                catch (System.Security.SecurityException ex)
                {
                    // This security exception will occur if the caller does not have
                    // some undefined set of SecurityPermission flags.
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }

                try
                {
                    ApplicationBaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                }
                catch (System.Security.SecurityException ex)
                {
                    // This security exception will occur if the caller does not have
                    // some undefined set of SecurityPermission flags.
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }

                #region Check for Unmanaged Code Permission Available

                // check whether the unmanaged code permission is available to avoid three potential stack walks
                SecurityPermission unmanagedCodePermission = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
                // avoid a stack walk by checking for the permission on the current assembly. this is safe because there are no
                // stack walk modifiers before the call.
                if (SecurityManager.IsGranted(unmanagedCodePermission))
                {
                    try
                    {
                        unmanagedCodePermission.Demand();
                        UnmanagedCodePermissionAvailable = true;
                    }
                    catch (SecurityException e)
                    {
                        if (ChoTrace.ChoSwitch.TraceError)
                        {
                            Trace.WriteLine(ChoApplicationException.ToString(e));
                        }
                    }
                }

                #endregion Check for Unmanaged Code Permission Available

                EventLogSourceName = ChoApplicationSettings.State.EventLogSourceName;

                #region GetApplicationName

                try
                {
                    ApplicationName = ChoApplicationSettings.State.ApplicationId;
                }
                catch (System.Security.SecurityException ex)
                {
                    // This security exception will occur if the caller does not have
                    // some undefined set of SecurityPermission flags.
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }

                    try
                    {
                        ApplicationName = System.IO.Path.GetFileName(EntryAssemblyLocation);
                    }
                    catch (System.Security.SecurityException e)
                    {
                        ChoTrace.Error(ChoApplicationException.ToString(e));
                    }
                }

                ApplicationNameWithoutExtension = Path.GetFileNameWithoutExtension(ApplicationName);
                if (!ChoApplicationSettings.State.LogFolder.IsNullOrWhiteSpace())
                {
                    ApplicationLogDirectory = ChoApplicationSettings.State.LogFolder;
                }
                else if (ChoApplicationSettings.State.UseApplicationDataFolderAsLogFolder)
                {
                    ApplicationLogDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ApplicationNameWithoutExtension, ChoReservedDirectoryName.Logs);
                }
                else
                {
                    ApplicationLogDirectory = Path.Combine(ApplicationBaseDirectory, ChoReservedDirectoryName.Logs);
                }

                #endregion GetApplicationName

                #region Get AppDomainName

                try
                {
                    AppDomainName = AppDomain.CurrentDomain.FriendlyName;
                }
                catch (Exception ex)
                {
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }

                #endregion Get AppDomainName

                #region Get ProcessId, ProcessName

                if (UnmanagedCodePermissionAvailable)
                {
                    try
                    {
                        ProcessId = ChoKernel32Core.GetCurrentProcessId();
                    }
                    catch (Exception ex)
                    {
                        if (ChoTrace.ChoSwitch.TraceError)
                        {
                            Trace.WriteLine(ex.ToString());
                        }
                    }

                    try
                    {
                        ProcessFilePath = GetProcessName();
                    }
                    catch (Exception ex)
                    {
                        if (ChoTrace.ChoSwitch.TraceError)
                        {
                            Trace.WriteLine(ex.ToString());
                        }
                    }
                }
                else
                {
                    if (ChoTrace.ChoSwitch.TraceError)
                    {
                        Trace.WriteLine("Failed to retrieve value due to unmanaged code permission denied.");
                    }
                }

                #endregion Get ProcessId, ProcessName

                #region Get HostName

                // Get the DNS host name of the current machine
                try
                {
                    // Lookup the host name
                    HostName = System.Net.Dns.GetHostName();
                }
                catch (System.Net.Sockets.SocketException)
                {
                }
                catch (System.Security.SecurityException)
                {
                    // We may get a security exception looking up the hostname
                    // You must have Unrestricted DnsPermission to access resource
                }

                // Get the NETBIOS machine name of the current machine
                if (HostName.IsNullOrWhiteSpace())
                {
                    try
                    {
                        HostName = Environment.MachineName;
                    }
                    catch (InvalidOperationException)
                    {
                    }
                    catch (System.Security.SecurityException)
                    {
                        // We may get a security exception looking up the machine name
                        // You must have Unrestricted EnvironmentPermission to access resource
                    }
                }

                #endregion Get HostName

                ApplyFrxParamsOverrides.Raise(this, null);
            }
        }
Ejemplo n.º 2
0
        private static void InitializeAppInfo()
        {
            RaiseApplyFrxParamsOverrides();

            try
            {
                EntryAssemblyLocation = ChoAssembly.GetEntryAssembly().Location;
                EntryAssemblyFileName = System.IO.Path.GetFileName(EntryAssemblyLocation);
            }
            catch (System.Security.SecurityException ex)
            {
                // This security exception will occur if the caller does not have
                // some undefined set of SecurityPermission flags.
                Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString());
            }

            if (!ServiceInstallation)
            {
                if (ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.SingleInstanceApp)
                {
                    Func <bool> verifyAnotherInstanceRunning = VerifyAnotherInstanceRunning;
                    if (verifyAnotherInstanceRunning != null)
                    {
                        bool instanceExists = verifyAnotherInstanceRunning();
                        if (instanceExists)
                        {
                            RaiseErrorOrActivateFirstInstance();
                        }
                    }
                    else
                    {
                        bool createdNew = true;
                        _singleInstanceMutex = new Mutex(true, ChoGlobalApplicationSettings.Me.ApplicationName, out createdNew);
                        if (!createdNew)
                        {
                            RaiseErrorOrActivateFirstInstance();
                        }
                    }
                }

                if (!ChoGlobalApplicationSettings.Me.TrayApplicationBehaviourSettings.TurnOn)
                {
                    if (ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.HideWindow)
                    {
                        ChoWindowsManager.Hide();
                    }
                    else
                    {
                        ChoWindowsManager.Show();

                        ChoWindowsManager.AlwaysOnTop(ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.AlwaysOnTop);

                        if (!ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.AlwaysOnTop)
                        {
                            if (ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.BringWindowToTop)
                            {
                                ChoWindowsManager.BringWindowToTop();
                            }
                        }
                        //ChoWindowManager.ShowInTaskbar(ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.ShowInTaskbar);
                    }
                }

                if (ChoApplicationHost.ApplicationContext != null)
                {
                    ChoApplicationHost.ApplicationContext.Visible = ChoGlobalApplicationSettings.Me.TrayApplicationBehaviourSettings.TurnOn;
                }
            }

            #region Check for Unmanaged Code Permission Available

            // check whether the unmanaged code permission is available to avoid three potential stack walks
            SecurityPermission unmanagedCodePermission = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
            // avoid a stack walk by checking for the permission on the current assembly. this is safe because there are no
            // stack walk modifiers before the call.
            if (SecurityManager.IsGranted(unmanagedCodePermission))
            {
                try
                {
                    unmanagedCodePermission.Demand();
                    UnmanagedCodePermissionAvailable = true;
                }
                catch (SecurityException ex)
                {
                    Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString());
                }
            }

            #endregion Check for Unmanaged Code Permission Available

            #region Get AppDomainName

            try
            {
                AppDomainName = AppDomain.CurrentDomain.FriendlyName;
            }
            catch (Exception ex)
            {
                Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString());
            }

            #endregion Get AppDomainName

            #region Get ProcessId, ProcessName

            if (UnmanagedCodePermissionAvailable)
            {
                try
                {
                    ProcessId = ChoKernel32Core.GetCurrentProcessId();
                }
                catch (Exception ex)
                {
                    Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString());

                    try
                    {
                        ProcessId = Process.GetCurrentProcess().Id;
                    }
                    catch { }
                }

                try
                {
                    ProcessFilePath = GetProcessName();
                }
                catch (Exception ex)
                {
                    Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString());
                }
            }
            else
            {
                try
                {
                    ProcessId = Process.GetCurrentProcess().Id;
                }
                catch { }

                Trace(ChoTrace.ChoSwitch.TraceError, "Failed to retrieve value due to unmanaged code permission denied.");
            }

            #endregion Get ProcessId, ProcessName

            ApplicationLogDirectory = ChoGlobalApplicationSettings.Me.ApplicationLogDirectory;

            if (!ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.SingleInstanceApp)
            {
                if (ChoGlobalApplicationSettings.Me.DoAppendProcessIdToLogDir)
                {
                    ApplicationLogDirectory = Path.Combine(ApplicationLogDirectory, ProcessId.ToString());
                }
            }

            Directory.CreateDirectory(ApplicationLogDirectory);
        }