-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
angelovstanton
committed
Nov 16, 2014
1 parent
7e48e50
commit 87ce95c
Showing
1,881 changed files
with
425,272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | ||
</startup> | ||
<appSettings> | ||
<add key="testKey " value="1" /> | ||
<add key="testKey1" value="myTestValue" /> | ||
</appSettings> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<testsConfiguration> | ||
<cars> | ||
<car name="Audi" price="10000" color="Red"/> | ||
<car name="BMW" price="20000" color="White"/> | ||
</cars> | ||
</testsConfiguration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Configuration; | ||
using System.Reflection; | ||
using System.Xml; | ||
|
||
namespace AAngelov.Utilities.Configuration | ||
{ | ||
/// <summary> | ||
/// Contains static methods for settings file modification | ||
/// </summary> | ||
public static class ConfigModificator | ||
{ | ||
/// <summary> | ||
/// Writes the setting. | ||
/// </summary> | ||
/// <param name="key">The key.</param> | ||
/// <param name="value">The value.</param> | ||
/// <param name="configWriterSettings">The configuration writer settings.</param> | ||
/// <exception cref="System.InvalidOperationException">appSettings section not found in config file.</exception> | ||
public static void WriteSetting(string key, string value, ConfigModificatorSettings configWriterSettings) | ||
{ | ||
XmlDocument doc = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath); | ||
// retrieve appSettings node | ||
XmlNode rootNode = doc.SelectSingleNode(configWriterSettings.RootNode); | ||
|
||
if (rootNode == null) | ||
{ | ||
throw new InvalidOperationException("appSettings section not found in config file."); | ||
} | ||
|
||
try | ||
{ | ||
// select the 'note for edit' element that contains your key | ||
XmlElement elem = (XmlElement)rootNode.SelectSingleNode(string.Format(configWriterSettings.NodeForEdit, key)); | ||
elem.FirstChild.InnerText = value; | ||
doc.Save(configWriterSettings.ConfigPath); | ||
} | ||
catch | ||
{ | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Changes the value by key. | ||
/// </summary> | ||
/// <param name="key">The key.</param> | ||
/// <param name="value">The value.</param> | ||
/// <param name="attributeForChange">The attribute for change.</param> | ||
/// <param name="configWriterSettings">The configuration writer settings.</param> | ||
/// <exception cref="System.InvalidOperationException">appSettings section not found in config file.</exception> | ||
public static void ChangeValueByKey(string key, string value, string attributeForChange, ConfigModificatorSettings configWriterSettings) | ||
{ | ||
XmlDocument doc = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath); | ||
// retrieve the root node | ||
XmlNode rootNode = doc.SelectSingleNode(configWriterSettings.RootNode); | ||
|
||
if (rootNode == null) | ||
{ | ||
throw new InvalidOperationException("the root node section not found in config file."); | ||
} | ||
|
||
try | ||
{ | ||
// select the element that contains the key | ||
XmlElement elem = (XmlElement)rootNode.SelectSingleNode(string.Format(configWriterSettings.NodeForEdit, key)); | ||
elem.SetAttribute(attributeForChange, value); | ||
doc.Save(configWriterSettings.ConfigPath); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw ex; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Loads the configuration document. | ||
/// </summary> | ||
/// <param name="configFilePath">The configuration file path.</param> | ||
/// <returns></returns> | ||
/// <exception cref="System.Exception">No configuration file found.</exception> | ||
private static XmlDocument LoadConfigDocument(string configFilePath) | ||
{ | ||
XmlDocument doc = null; | ||
try | ||
{ | ||
doc = new XmlDocument(); | ||
doc.Load(configFilePath); | ||
return doc; | ||
} | ||
catch (System.IO.FileNotFoundException e) | ||
{ | ||
throw new Exception("No configuration file found.", e); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Refreshes the application settings. | ||
/// </summary> | ||
public static void RefreshAppSettings() | ||
{ | ||
ConfigurationManager.RefreshSection("appSettings"); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
AANGELOV/ConfigModificator/CSharp/ConfigModificatorSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
|
||
namespace AAngelov.Utilities.Configuration | ||
{ | ||
/// <summary> | ||
/// Contains Config Writer Setting Properties | ||
/// </summary> | ||
public class ConfigModificatorSettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets the application settings node. | ||
/// </summary> | ||
/// <value> | ||
/// The application settings node. | ||
/// </value> | ||
public string RootNode { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the node for edit. | ||
/// </summary> | ||
/// <value> | ||
/// The node for edit. | ||
/// </value> | ||
public string NodeForEdit { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the configuration path. | ||
/// </summary> | ||
/// <value> | ||
/// The configuration path. | ||
/// </value> | ||
public string ConfigPath { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConfigModificatorSettings"/> class. | ||
/// </summary> | ||
/// <param name="appSettingsNode">The application settings node.</param> | ||
/// <param name="nodeForEdit">The node for edit.</param> | ||
/// <param name="configPath">The configuration path.</param> | ||
public ConfigModificatorSettings(String appSettingsNode, String nodeForEdit, string configPath) | ||
{ | ||
this.RootNode = appSettingsNode; | ||
this.NodeForEdit = nodeForEdit; | ||
this.ConfigPath = configPath; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConfigModificatorSettings"/> class. | ||
/// </summary> | ||
/// <param name="appSettingsNode">The application settings node.</param> | ||
/// <param name="configPath">The configuration path.</param> | ||
public ConfigModificatorSettings(String appSettingsNode, string configPath) | ||
{ | ||
this.RootNode = appSettingsNode; | ||
this.ConfigPath = configPath; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using AAngelov.Utilities.Configuration; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace TestConfigModificator.Console | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
//Example how to change app.config file associated to your application. | ||
//You can use the same approach for web.configs and other type of configurations | ||
string appConfigFilePath = string.Concat(Assembly.GetExecutingAssembly().Location, ".config"); | ||
ConfigModificatorSettings appConfigWriterSettings = | ||
new ConfigModificatorSettings("//appSettings", "//add[@key='{0}']", appConfigFilePath); | ||
|
||
string value = ConfigurationManager.AppSettings["testKey1"]; | ||
System.Console.WriteLine("Value before modification: {0}", value); | ||
|
||
ConfigModificator.ChangeValueByKey( | ||
key: "testKey1", | ||
value: "ChangedValueByModificator", | ||
attributeForChange: "value", | ||
configWriterSettings: appConfigWriterSettings); | ||
|
||
ConfigModificator.RefreshAppSettings(); | ||
value = ConfigurationManager.AppSettings["testKey1"]; | ||
System.Console.WriteLine("Value after modification: {0}", value); | ||
|
||
//Example how to change Custom XML configuration | ||
string carsConfigFilePath = "Cars.xml"; | ||
ConfigModificatorSettings carsConfigWriterSettings = | ||
new ConfigModificatorSettings("//cars", "//car[@name='{0}']", carsConfigFilePath); | ||
|
||
ConfigModificator.ChangeValueByKey( | ||
key: "BMW", | ||
value: "Mazda", | ||
attributeForChange: "name", | ||
configWriterSettings: carsConfigWriterSettings); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | ||
</startup> | ||
<appSettings> | ||
<add key="testKey " value="1" /> | ||
<add key="testKey1" value="myTestValue" /> | ||
</appSettings> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<testsConfiguration> | ||
<cars> | ||
<car name="Audi" price="10000" color="Red"/> | ||
<car name="BMW" price="20000" color="White"/> | ||
</cars> | ||
</testsConfiguration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
Imports System.Configuration | ||
Imports System.Reflection | ||
Imports System.Xml | ||
|
||
Namespace AAngelov.Utilities.Configuration | ||
''' <summary> | ||
''' Contains static methods for settings file modification | ||
''' </summary> | ||
Public NotInheritable Class ConfigModificator | ||
Private Sub New() | ||
End Sub | ||
''' <summary> | ||
''' Writes the setting. | ||
''' </summary> | ||
''' <param name="key">The key.</param> | ||
''' <param name="value">The value.</param> | ||
''' <param name="configWriterSettings">The configuration writer settings.</param> | ||
''' <exception cref="System.InvalidOperationException">appSettings section not found in config file.</exception> | ||
Public Shared Sub WriteSetting(key As String, value As String, configWriterSettings As ConfigModificatorSettings) | ||
Dim doc As XmlDocument = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath) | ||
' retrieve appSettings node | ||
Dim rootNode As XmlNode = doc.SelectSingleNode(configWriterSettings.RootNode) | ||
|
||
If rootNode Is Nothing Then | ||
Throw New InvalidOperationException("appSettings section not found in config file.") | ||
End If | ||
|
||
Try | ||
' select the 'note for edit' element that contains your key | ||
Dim elem As XmlElement = DirectCast(rootNode.SelectSingleNode(String.Format(configWriterSettings.NodeForEdit, key)), XmlElement) | ||
elem.FirstChild.InnerText = value | ||
doc.Save(configWriterSettings.ConfigPath) | ||
Catch | ||
Throw | ||
End Try | ||
End Sub | ||
|
||
''' <summary> | ||
''' Changes the value by key. | ||
''' </summary> | ||
''' <param name="key">The key.</param> | ||
''' <param name="value">The value.</param> | ||
''' <param name="attributeForChange">The attribute for change.</param> | ||
''' <param name="configWriterSettings">The configuration writer settings.</param> | ||
''' <exception cref="System.InvalidOperationException">appSettings section not found in config file.</exception> | ||
Public Shared Sub ChangeValueByKey(key As String, value As String, attributeForChange As String, configWriterSettings As ConfigModificatorSettings) | ||
Dim doc As XmlDocument = ConfigModificator.LoadConfigDocument(configWriterSettings.ConfigPath) | ||
' retrieve the root node | ||
Dim rootNode As XmlNode = doc.SelectSingleNode(configWriterSettings.RootNode) | ||
|
||
If rootNode Is Nothing Then | ||
Throw New InvalidOperationException("the root node section not found in config file.") | ||
End If | ||
|
||
Try | ||
' select the element that contains the key | ||
Dim elem As XmlElement = DirectCast(rootNode.SelectSingleNode(String.Format(configWriterSettings.NodeForEdit, key)), XmlElement) | ||
elem.SetAttribute(attributeForChange, value) | ||
doc.Save(configWriterSettings.ConfigPath) | ||
Catch ex As Exception | ||
Throw ex | ||
End Try | ||
End Sub | ||
|
||
''' <summary> | ||
''' Loads the configuration document. | ||
''' </summary> | ||
''' <param name="configFilePath">The configuration file path.</param> | ||
''' <returns></returns> | ||
''' <exception cref="System.Exception">No configuration file found.</exception> | ||
Private Shared Function LoadConfigDocument(configFilePath As String) As XmlDocument | ||
Dim doc As XmlDocument = Nothing | ||
Try | ||
doc = New XmlDocument() | ||
doc.Load(configFilePath) | ||
Return doc | ||
Catch e As System.IO.FileNotFoundException | ||
Throw New Exception("No configuration file found.", e) | ||
End Try | ||
End Function | ||
|
||
''' <summary> | ||
''' Refreshes the application settings. | ||
''' </summary> | ||
Public Shared Sub RefreshAppSettings() | ||
ConfigurationManager.RefreshSection("appSettings") | ||
End Sub | ||
End Class | ||
End Namespace |
Oops, something went wrong.