2 // System.Configuration.ConfigurationManager.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Lluis Sanchez Gual (lluis@novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
31 using System.Collections;
32 using System.Runtime.CompilerServices;
33 using System.Collections.Specialized;
34 using System.Reflection;
38 using System.Configuration.Internal;
40 namespace System.Configuration {
42 /*roaming user config path: C:\Documents and Settings\toshok\Application Data\domain-System.Configurati_Url_py3nlovv3wxe21qgacxc3n2b1mph2log\1.0.0.0\user.config */
44 public static class ConfigurationManager
46 static InternalConfigurationFactory configFactory = new InternalConfigurationFactory ();
47 static IInternalConfigSystem configSystem = new ClientConfigurationSystem ();
48 static object lockobj = new object ();
50 [MonoTODO ("Evidence and version still needs work")]
51 static string GetAssemblyInfo (Assembly a)
60 attrs = a.GetCustomAttributes (typeof (AssemblyProductAttribute), false);
61 if (attrs != null && attrs.Length > 0)
62 app_name = ((AssemblyProductAttribute)attrs[0]).Product;
64 app_name = AppDomain.CurrentDomain.FriendlyName;
66 sb = new StringBuilder();
68 sb.Append ("evidencehere");
70 evidence_str = sb.ToString();
72 attrs = a.GetCustomAttributes (typeof (AssemblyVersionAttribute), false);
73 if (attrs != null && attrs.Length > 0)
74 version = ((AssemblyVersionAttribute)attrs[0]).Version;
76 version = "1.0.0.0" /* XXX */;
79 return Path.Combine (String.Format ("{0}_{1}", app_name, evidence_str), version);
82 internal static Configuration OpenExeConfigurationInternal (ConfigurationUserLevel userLevel, Assembly calling_assembly, string exePath)
84 if (calling_assembly == null && exePath == null)
85 throw new ArgumentException ("exePath must be specified when not running inside a stand alone exe.");
87 ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
89 /* Roaming and RoamingAndLocal should be different
92 PerUserRoaming = \Documents and Settings\<username>\Application Data\...
93 PerUserRoamingAndLocal = \Documents and Settings\<username>\Local Settings\Application Data\...
97 case ConfigurationUserLevel.None:
99 exePath = calling_assembly.Location;
100 else if (!File.Exists (exePath))
104 if (!exePath.EndsWith (".config"))
105 map.ExeConfigFilename = exePath + ".config";
107 map.ExeConfigFilename = exePath;
110 case ConfigurationUserLevel.PerUserRoaming:
111 map.RoamingUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), GetAssemblyInfo(calling_assembly));
112 map.RoamingUserConfigFilename = Path.Combine (map.RoamingUserConfigFilename, "user.config");
113 goto case ConfigurationUserLevel.PerUserRoamingAndLocal;
115 case ConfigurationUserLevel.PerUserRoamingAndLocal:
116 map.LocalUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), GetAssemblyInfo(calling_assembly));
117 map.LocalUserConfigFilename = Path.Combine (map.LocalUserConfigFilename, "user.config");
121 return ConfigurationFactory.Create (typeof(ExeConfigurationHost), map);
124 public static Configuration OpenExeConfiguration (ConfigurationUserLevel userLevel)
126 return OpenExeConfigurationInternal (userLevel, Assembly.GetCallingAssembly (), null);
129 public static Configuration OpenExeConfiguration (string exePath)
131 return OpenExeConfigurationInternal (ConfigurationUserLevel.None, Assembly.GetCallingAssembly (), exePath);
134 [MonoTODO ("userLevel")]
135 public static Configuration OpenMappedExeConfiguration (ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
137 return ConfigurationFactory.Create (typeof(ExeConfigurationHost), fileMap);
140 public static Configuration OpenMachineConfiguration ()
142 ConfigurationFileMap map = new ConfigurationFileMap ();
143 return ConfigurationFactory.Create (typeof(MachineConfigurationHost), map);
146 public static Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
148 return ConfigurationFactory.Create (typeof(MachineConfigurationHost), fileMap);
151 internal static IInternalConfigConfigurationFactory ConfigurationFactory {
152 get { return configFactory; }
155 internal static IInternalConfigSystem ConfigurationSystem {
156 get { return configSystem; }
159 public static object GetSection (string sectionName)
161 object o = ConfigurationSystem.GetSection (sectionName);
162 if (o is ConfigurationSection)
163 return ((ConfigurationSection) o).GetRuntimeObject ();
168 public static void RefreshSection (string sectionName)
170 ConfigurationSystem.RefreshConfig (sectionName);
173 public static NameValueCollection AppSettings {
175 return (NameValueCollection) GetSection ("appSettings");
180 public static ConnectionStringSettingsCollection ConnectionStrings {
182 ConnectionStringsSection connectionStrings = (ConnectionStringsSection) GetSection ("connectionStrings");
183 return connectionStrings.ConnectionStrings;
187 /* invoked from System.Web */
188 static IInternalConfigSystem ChangeConfigurationSystem (IInternalConfigSystem newSystem)
190 if (newSystem == null)
191 throw new ArgumentNullException ("newSystem");
194 IInternalConfigSystem old = configSystem;
195 configSystem = newSystem;