I'm trying to get data from my App.config and searching about I have found up to 4 ways to get that data, but basically you should use
ConfigurationManager.AppSettings["NameKey/NameSetting"];
My App.config code is
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="DXThemeManager" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="InterfazPrueba1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<DXThemeManager>
<setting name="ApplicationThemeName" serializeAs="String">
<value>Office2016White</value>
</setting>
</DXThemeManager>
</userSettings>
<applicationSettings>
<InterfazPrueba1.Properties.Settings>
<setting name="HostBD" serializeAs="String">
<value>LOCALHOST</value>
</setting>
<setting name="NombreBD" serializeAs="String">
<value>Pruebas1</value>
</setting>
<setting name="ServerBD" serializeAs="String">
<value>DESKTOP-O037RKH</value>
</setting>
<setting name="InstanceBD" serializeAs="String">
<value />
</setting>
<setting name="UsuarioBD" serializeAs="String">
<value>sa</value>
</setting>
<setting name="PassBD" serializeAs="String">
<value>123</value>
</setting>
</InterfazPrueba1.Properties.Settings>
</applicationSettings>
</configuration>
And the code in my class is:
private void datosConexion()
{
server = ConfigurationManager.AppSettings["ServerBD"];
user = ConfigurationManager.AppSettings["UsuarioBD"];
pass = ConfigurationManager.AppSettings["PasswordBD"];
host = ConfigurationManager.AppSettings["HostBD"];
db= ConfigurationManager.AppSettings["NombreBD"];
instance = ConfigurationManager.AppSettings["InstanceBD"];
Console.WriteLine("Datos:"+server + user + pass + host + db + instance);
}
All the variables have nothing.
I've tried all the ways I've seen and changed the values to keys but I just can retrieve the data...
Any idea about what is wrong or what am I doing bad?
Thank you for all the support you could give me.
ConfigurationManager.AppSettings["NameSetting"] retrives a setting that is defined in the App.config like this:
<configuration>
<configSections>
...
</configSections>
<userSettings>
...
</userSettings>
<appSettings>
<add key="NameSetting" value="123" />
</appSettings>
</configuration>
If you add project specific settings under Project->Properties->Settings you can access these through a class that gets generated for you:
string setting = InterfazPrueba1.Properties.Settings.Default.HostBD;
Related
I have the following code structure:
xUnit tests
These use features from my.dll
The my.dll uses a third party logger.dll
If I use the my.dll outside of xUnit's testserver.dll, then I can just add a my.dll.config with the following contents:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Thirdparty.Logger.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<assemblySettings>
<add key="Hostname" value="127.0.0.1"/>
<add key="Port" value="1234"/>
</assemblySettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
<applicationSettings>
<Thirdparty.Logger.Properties.Settings>
<setting name="LogPath" serializeAs="String">
<value>C:\log\dir</value>
</setting>
</Thirdparty.Logger.Properties.Settings>
</applicationSettings>
</configuration>
And within my.dll's constructor, I can read them like follows, which works just fine.
AssemblySettings settings = new AssemblySettings();
var host = settings["Hostname"];
var port = settings["Port"];
Also the LogPath is passed on to logger.dll, which is closed source to me, but the logger.dll can definitely read the value passed on as LogPath.
Now, I have been successfully able to pass on the logger.dll's settings through xUnit as well using a testserver.dll.config file, which looks like follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Thirdparty.Logger.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
<section name="MyDll.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
<applicationSettings>
<Thirdparty.Logger.Properties.Settings>
<setting name="LogPath" serializeAs="String">
<value>C:\log\dir</value>
</setting>
</Thirdparty.Logger.Properties.Settings>
<MyDll.Properties.Settings>
<setting name="Hostname" serializeAs="String">
<value>127.0.0.1</value>
</setting>
<setting name="Port" serializeAs="String">
<value>1234</value>
</setting>
</MyDll.Properties.Settings>
</applicationSettings>
</configuration>
And also in this case, the LogPath is successfully passed on, but the settings for my.dll.config are not. The AssemblySettings instance is null:
AssemblySettings settings = new AssemblySettings();
// settings == null
What am I doing wrong?
Assembly.GetCallingAssembly().FullName in the constructor of AssemblySettings evaluates to "MyDll, Version=6.1.0.0, Culture=neutral, PublicKeyToken=null", but the returned instance is null.
I've been struggling with multiple toolstrips in a toolstripcontainer for years.
I have a current .NETv3.5 application written in VS2008 where the location of the toolstrips are random. A lot of custom code have been made to fix this but without any luck.
Currently I'm working in VS2012 on a .NETv4.5 application which also has multiple toolstrips.
I created a very small test application that is a form with a docked toolstripcontainer and 4 toolstrips. At design time I added 3 toolstrips to the bottom and 1 to the right.
Without adding any code and starting this application, my toolstrips are located at the same location as in design time. Now I reorder the 3 top toolstrips and restart the application.
My reorder changes are not saved the toolstrips are again as in design time.
Now I add these lines of code:
public partial class Toolstrips : Form
{
private string keyName;
public Toolstrips()
{
this.InitializeComponent();
this.keyName = Application.ProductName + this.Name + "xyz";
ToolStripManager.LoadSettings(this, this.keyName);
}
private void Toolstrips_FormClosing(object sender, FormClosingEventArgs e)
{
ToolStripManager.SaveSettings(this, this.keyName);
}
}
And still in VS2102 with .NETv4.5 this is not working.
I can reorder what I want after restarting the application all toolstrips are restored to the design time locations.
I had hoped this would be fixed in VS2012 but apparently not.
Does anybody have a work around to get this to work?
EDIT: Added contents user.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripTable" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripPan" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripStandard" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripZoom" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripTable>
<setting name="ItemOrder" serializeAs="String">
<value>toolStripButton4</value>
</setting>
<setting name="IsDefault" serializeAs="String">
<value>False</value>
</setting>
<setting name="Size" serializeAs="String">
<value>29, 42</value>
</setting>
<setting name="ToolStripPanelName" serializeAs="String">
<value>toolStripContainer1.Right</value>
</setting>
<setting name="Visible" serializeAs="String">
<value>True</value>
</setting>
<setting name="Location" serializeAs="String">
<value>0, 336</value>
</setting>
<setting name="Name" serializeAs="String">
<value>toolStripTable</value>
</setting>
</System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripTable>
<System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripPan>
<setting name="ItemOrder" serializeAs="String">
<value>toolStripButton3</value>
</setting>
<setting name="IsDefault" serializeAs="String">
<value>False</value>
</setting>
<setting name="Size" serializeAs="String">
<value>40, 31</value>
</setting>
<setting name="ToolStripPanelName" serializeAs="String">
<value>toolStripContainer2.Top</value>
</setting>
<setting name="Visible" serializeAs="String">
<value>False</value>
</setting>
<setting name="Location" serializeAs="String">
<value>288, 0</value>
</setting>
<setting name="Name" serializeAs="String">
<value>toolStripPan</value>
</setting>
</System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripPan>
<System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripStandard>
<setting name="ItemOrder" serializeAs="String">
<value>newToolStripButton,openToolStripButton,saveToolStripButton,printToolStripButton,toolStripSeparator,cutToolStripButton,copyToolStripButton,pasteToolStripButton,toolStripSeparator1,helpToolStripButton</value>
</setting>
<setting name="IsDefault" serializeAs="String">
<value>False</value>
</setting>
<setting name="Size" serializeAs="String">
<value>248, 31</value>
</setting>
<setting name="ToolStripPanelName" serializeAs="String">
<value>toolStripContainer2.Top</value>
</setting>
<setting name="Visible" serializeAs="String">
<value>False</value>
</setting>
<setting name="Location" serializeAs="String">
<value>6, 0</value>
</setting>
<setting name="Name" serializeAs="String">
<value>toolStripStandard</value>
</setting>
</System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripStandard>
<System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripZoom>
<setting name="ItemOrder" serializeAs="String">
<value>toolStripButton1,toolStripButton2</value>
</setting>
<setting name="IsDefault" serializeAs="String">
<value>False</value>
</setting>
<setting name="Size" serializeAs="String">
<value>29, 73</value>
</setting>
<setting name="ToolStripPanelName" serializeAs="String">
<value>toolStripContainer2.Right</value>
</setting>
<setting name="Visible" serializeAs="String">
<value>False</value>
</setting>
<setting name="Location" serializeAs="String">
<value>0, 71</value>
</setting>
<setting name="Name" serializeAs="String">
<value>toolStripZoom</value>
</setting>
</System.Windows.Forms.ToolStripSettings.GUI-prototypeToolstripsxyz.toolStripZoom>
</userSettings>
</configuration>
EDIT2
I've created a small VS2012 application written in C# to illustrate my problem. You can download it here: https://mapwindow5.svn.codeplex.com/svn/tmp/ToolstripDemo/ToolstripDemo.zip
When I start the application I see:
When I reorder the 3 top toolstrips I get this:
Now I close the application and reopen it again, I get the initial locations:
Now I move the toolstrip which was docked at the right to the top:
I reopen the application and I see the 4th toolstrip is still at the top, which is OK but the order is not. Look at the two last toolstrips:
Another strange thing is that I cannot redock my zoom-toolstrip to the right again. The toolstrip just disappears:
When I open my form in VS2012 the toolstrips are rearranged as well:
I'm not sure why. I'm just reopening the form.
I did my testing and made the screenshots by running the exe from the explorer, not in debug-mode of VS.
I hope it is now more clear what I mean and what I need.
Any advice is very much appreciated.
I use vb.net 2010, so I use vb's syntax, but i hope my answer helps:
1) Using ToolStripManager, You can save and load settings without specifying a key for all of toolbars seperately. Specifying a form is enough - settings will be saved for a form using form's name as a key.
2) It is important to call ToolStripManager.SaveSettings() and ToolStripManager.LoadSettings() not prepended by something else programaticaly changing toolbars layout at runtime in FormLoad and FormClosing event-run subroutines. So most safe way to do this, is just call the methods as first in FormClosing and FormLoad subs, as below:
Private Sub Explorer1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ToolStripManager.LoadSettings(Me)'Me is the form, for which we save the toolbars settings
'further code in this subroutine
End Sub
Private Sub Explorer1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
ToolStripManager.SaveSettings(Me)
My.Settings.Save()
'further code in this subroutine
End Sub
I have a C# application where I store certain value in a Settings file like this:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="VITRIconEvacuationPlan.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="VITRIconEvacuationPlan.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<EvacuationPlan.Properties.Settings>
<setting name="AssemblyCentre" serializeAs="String">
<value>False</value>
</setting>
</EvacuationPlan.Properties.Settings>
</applicationSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><userSettings>
<EvacuationPlan.Properties.Settings>
<setting name="SymbolScale" serializeAs="String">
<value>25</value>
</setting>
</EvacuationPlan.Properties.Settings>
</userSettings>
</configuration>
Per default, the SymbolScale property is set to 25 (when I start the application the first time)
I want to change the SymbolScale property at runtime so I put this into User Scope. So I can say:
setting.SymbolScale = 150;
setting.save();
But when I close my application the value of the SymbolScale is 25 again. But I want it to store my chenged value from the runtime. What am I doing wrong?
I found a solution here:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ddeaca86-a093-4997-82c9-01bc0c630138/
I just had to change and save my SymbolValue like that:
Properties.Settings.Default.SymbolScale = 150;
Properties.Settings.Default.Save();
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.Save();
I am not sure why I have to call the save function two times but it works (And only with calling it two times)
Are you sure that you are checking the right "user.config" file?
At usual that stored in "C:\Users\xyzuser\AppData\Local\yourcompany\youarpp\version\user.config".
Hope that helps!
Set it like: Settings.Default.SymbolScale = 150;
Save it with Settings.Default.Save();
I have a WinForms application that I have changed the name of. I've changed the name everywhere I can find it, and can find no traces of the old name (except as below) and everything works fine; the one exception is in app.config, where if I change OLDNAME to NEWNAME any attempt to read any of the configuration settings throws a ConfigurationExceptioon of "Configuration system not ready", if I recall correctly.
Can anyone suggest how I can change the OLDNAME below to something else, such as my new name, without upsetting the config system?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="OLDNAME.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<OLDNAME.Properties.Settings>
<setting name="MainWindowPosition" serializeAs="String">
<value>0, 0, 0, 0</value>
</setting>
<setting name="UseShortDeviceNames" serializeAs="String">
<value>False</value>
</setting>
</OLDNAME.Properties.Settings>
</userSettings>
</configuration>
The link http://www.codeproject.com/Articles/10981/Understanding-Section-Handlers-App-config-File will explain more using Section Handlers for config.
You must change alsow the section name, you section name is linked to the projects where is build and you have two posibility:
- rename the project (output) and in app.config
- create a new custom group and use the settings
I am trying to get the value for the key "sUser" in appSetting section from app.config
this is the code on my class.cs
string sUsr = ConfigurationManager.AppSettings.Get["sUser"];
But always its null.
I try this code (I took it from MSDN):
// Get the configuration file.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get the appSettings section.
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
// Get the auxiliary file name.
Console.WriteLine("Auxiliary file: {0}", config.AppSettings.File);
But it shows that the file Empty
Also I am using the stting console to write those properties and it works if I get the apps using
string sUsr = WIW.Business.Properties.Settings.Default.sUser;
But I am going to use that class as a reference in a website and on the web.config of the site I can't configure those settings
In your App.config the appSettings section should look like the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="sUser" value="Test"/>
</appSettings>
</configuration>
use Properties.Settings.Default.{your_setting_key} to access the value
I.E
Properties.Settings.Default.sPassword return 8909039388
All these were included in Properties Class.
I had the same problem. I was calling another project(In the same solution) and was expecting the appSettings to be present.
To make it work,I added the following lines in the application that was calling the target application.
<appSettings>
<add key="sUser" value="Test"/>
</appSettings>
It worked for me after that. Try this. It might help you too
Here its my app.config, if I try to get the key values from the appSettings sections I always get null, but If I try to get them from applicationsection Its no problem, the thing is that I need put that applicationsection on a web site but on a web site i can put applicationSettings
<configuration>
<appSettings>
<add key="sUser" value="userbancolombia">
<add key="sPassword" value="8909039388">
<appSettings>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WIW.Business.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.serviceModel>
<bindings />
<client />
</system.serviceModel>
<applicationSettings>
<WIW.Business.Properties.Settings>
<setting name="WIW_Business_WsCCMain_ConsultaNuips" serializeAs="String">
<value>http://coomeva1/AsisaWS_2008/ConsultaNuips.asmx</value>
</setting>
<setting name="WIW_Business_WsCCAux_ConsultaNuips" serializeAs="String">
<value>http://coomeva1/AsisaWS_2008_Sec/ConsultaNuips.asmx</value>
</setting>
<setting name="WIW_Business_WsCEMain_ConsultaCedulaExtranjeria"
serializeAs="String">
<value>http://coomeva1/AsisaWS_2008/ConsultaCedulaExtranjeria.asmx</value>
</setting>
<setting name="WIW_Business_WsCEAux_ConsultaCedulaExtranjeria"
serializeAs="String">
<value>http://coomeva1/AsisaWS_2008_Sec/ConsultaCedulaExtranjeria.asmx</value>
</setting>
<setting name="sUser" serializeAs="String">
<value>userbancolombia</value>
</setting>
<setting name="sPassword" serializeAs="String">
<value>8909039388</value>
</setting>
</WIW.Business.Properties.Settings>
</applicationSettings>
</configuration>