コード例 #1
0
ファイル: ModuleInitializer.cs プロジェクト: voland-xp/Catel
        private static Configuration GetExeConfiguration()
        {
            Configuration config = null;

            if (HttpContextHelper.HasHttpContext())
            {
                Log.Debug("Application is living in a web context, loading web configuration");

                // All via reflection because we are support .NET 4.0 client profile, reflection equals this call:
                //   config = Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                var webConfigurationManagerType    = TypeCache.GetTypeWithoutAssembly("System.Web.Configuration.WebConfigurationManager", allowInitialization: false);
                var openWebConfigurationMethodInfo = webConfigurationManagerType.GetMethodEx("OpenWebConfiguration", new[] { typeof(string) }, allowStaticMembers: true);
                config = (Configuration)openWebConfigurationMethodInfo.Invoke(null, new[] { "~" });
            }
            else
            {
                Log.Debug("Application is living in an application context, loading application configuration");

                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (ContainsVsHost(config.FilePath))
                {
                    return(null);
                }
            }

            return(config);
        }
コード例 #2
0
ファイル: FileLogListener.cs プロジェクト: wushian/Catel
        /// <summary>
        /// Determines the real file path.
        /// </summary>
        /// <param name="filePath">The file path to examine.</param>
        /// <returns>The real file path.</returns>
        protected virtual string DetermineFilePath(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                filePath = string.Empty;
            }

            if (filePath.Contains(FilePathKeyword.AutoLogFileName))
            {
                filePath = filePath.Replace(FilePathKeyword.AutoLogFileName, AutoLogFileNameReplacement);
            }

            var isWebApp = HttpContextHelper.HasHttpContext();

            string dataDirectory;

            if (_assembly != null)
            {
                dataDirectory = isWebApp ? IO.Path.GetApplicationDataDirectoryForAllUsers(_assembly.Company(), _assembly.Product())
                                         : IO.Path.GetApplicationDataDirectory(_assembly.Company(), _assembly.Product());
            }
            else
            {
                dataDirectory = isWebApp ? IO.Path.GetApplicationDataDirectoryForAllUsers()
                                         : IO.Path.GetApplicationDataDirectory();
            }

            if (_assembly != null && filePath.Contains(FilePathKeyword.AssemblyName))
            {
                filePath = filePath.Replace(FilePathKeyword.AssemblyName, _assembly.GetName().Name);
            }

            if (_assembly != null && filePath.Contains(FilePathKeyword.AssemblyProduct))
            {
                filePath = filePath.Replace(FilePathKeyword.AssemblyProduct, _assembly.Product());
            }

            if (_assembly != null && filePath.Contains(FilePathKeyword.AssemblyCompany))
            {
                filePath = filePath.Replace(FilePathKeyword.AssemblyCompany, _assembly.Company());
            }

            if (filePath.Contains(FilePathKeyword.ProcessId))
            {
                filePath = filePath.Replace(FilePathKeyword.ProcessId, Process.GetCurrentProcess().Id.ToString());
            }

            if (filePath.Contains(FilePathKeyword.Date))
            {
                filePath = filePath.Replace(FilePathKeyword.Date, DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
            }

            if (filePath.Contains(FilePathKeyword.Time))
            {
                filePath = filePath.Replace(FilePathKeyword.Time, DateTime.Now.ToString("HHmmss", CultureInfo.InvariantCulture));
            }

            if (filePath.Contains(FilePathKeyword.AppData))
            {
                filePath = filePath.Replace(FilePathKeyword.AppData, dataDirectory);
            }

            if (filePath.Contains(FilePathKeyword.AppDataLocal))
            {
                var dataDirectoryLocal = _assembly != null?IO.Path.GetApplicationDataDirectory(ApplicationDataTarget.UserLocal,
                                                                                               _assembly.Company(),
                                                                                               _assembly.Product())
                                             : IO.Path.GetApplicationDataDirectory(ApplicationDataTarget.UserLocal);


                filePath = filePath.Replace(FilePathKeyword.AppDataLocal, dataDirectoryLocal);
            }

            if (filePath.Contains(FilePathKeyword.AppDataRoaming))
            {
                var dataDirectoryRoaming = _assembly != null?IO.Path.GetApplicationDataDirectory(ApplicationDataTarget.UserRoaming,
                                                                                                 _assembly.Company(),
                                                                                                 _assembly.Product())
                                               : IO.Path.GetApplicationDataDirectory(ApplicationDataTarget.UserRoaming);


                filePath = filePath.Replace(FilePathKeyword.AppDataRoaming, dataDirectoryRoaming);
            }

            if (filePath.Contains(FilePathKeyword.AppDataMachine))
            {
                var dataDirectoryMachine = _assembly != null?IO.Path.GetApplicationDataDirectory(ApplicationDataTarget.Machine,
                                                                                                 _assembly.Company(),
                                                                                                 _assembly.Product())
                                               : IO.Path.GetApplicationDataDirectory(ApplicationDataTarget.Machine);

                filePath = filePath.Replace(FilePathKeyword.AppDataMachine, dataDirectoryMachine);
            }

            if (filePath.Contains(FilePathKeyword.AppDir))
            {
                filePath = filePath.Replace(FilePathKeyword.AppDir, AppDomain.CurrentDomain.BaseDirectory);
            }

            if (filePath.Contains(FilePathKeyword.WorkDir))
            {
                filePath = filePath.Replace(FilePathKeyword.WorkDir, Directory.GetCurrentDirectory());
            }

            filePath = IO.Path.GetFullPath(filePath, dataDirectory);

            if (!filePath.EndsWith(".log"))
            {
                filePath += ".log";
            }

            return(filePath);
        }