How does AppSettings in VB.NET windows Forms work? - c#

Hello I have build a small app to demo a concept in C# in which I added the application config file etc added System.Configuration dll to the reference and accessed the settings:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="username" value="myknownusername"/>
</appSettings>
</configuration>
No news It worked flawlessly with the code:
private String username = ConfigurationManager.AppSettings["username"];
Now I have been asked to do the port the app to VB.NET and to my biggest surprise. It's been a while that I opened visual studio in VB.NET project. I was surprised to see an already white dashed App.config file so I added my AppSettings section and after 3 hours I still can't get the value of the username using the same ConfigurationManager
Dim username as String = ConfigurationManager.AppSettings("username")
I have included another application configuration app1.config which by the way has generated entries already. I could not get anything with the app1.config either.It also returns Nothing
</sharedListeners>
</system.diagnostics>
</configuration>
I am really perplexed as how a simple reading of configuration file could be this challenging in VB.NET unless I am using the wrong method which I always use in C#.
Kindly point me to whatever I am not doing right.
EDIT
As you can see in the picture below, I have all it needs to work properly.I as expecting to read the setting key from either App.config or app1.config. When I run like shown below the MessageBox is empty

I found the culprit, I needed to right click the generated App.config and choose Include In Project. that's it. it's weird because I never do that in a C# project.

Related

App.config Not using Values outside of Visual Studio

I'm new to C#. In the past projects I have worked on, I inherited code where the user already created some default config file system to allow a user to be able to input their own settings outside of the developer environment. Where all they need is the exe and config file which they can edit.
I am writing a simple file copier application and it dawned on me that the two projects I've worked on the users went out of their way to write their own way to use config files. To save time, I looked and saw that Visual Studio gives you a default app.config file to use. So I tried to use it.
My issue:
I have this bit of code:
public static void GetBaseSoureDirSubFolders()
{
BaseSourcePath = ConfigurationManager.AppSettings.Get("BaseSourcePath");
Console.WriteLine($"Base Dir Is: { BaseSourcePath}");
DirectoryInfo dirInfo = new DirectoryInfo(BaseSourcePath);
BaseSubFolders = dirInfo.GetDirectories();
}
When I run the code inside visual studio with my appconfig file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key="BaseSourcePath" value="C:\TDX2KlarfOutputs\"/>
<add key="share" value="\\cdserver.com\share\"/>
<add key="UserCount" value="2"/>
</appSettings>
</configuration>
it does return the value I have inside the file that I wrote in Visual studio.
The problem happens when I take the exe file and the app.config file and put it in the location I want to run this code. When I run the code, it gives me C:\TDX2KlarfOutputs\ instead of the new value I put inside app.config for that BaseSourcePath. It still uses the one I set in Visual studio.
THings I have tried:
I have set it to app.config properties to Build Action =content and CopytoOutputDirector = copy if newer.
I have tried using this in my main() function :
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", #".\App.config");
But this doesn't work. I am very new to C# so I'm probably not using the right terminology to describe the issue. The issue is, how can I make my console app, use values set by user on their copy of app.config file outside of Visual studio's values?
I have tried to google this but maybe due to my lack of experience I cannot find a question that answers my issue.
I think you are not using the compiled app.config but instead the uncompiled file.
If your program (.exe) name is copier, it should create a copier.app.config file.
Try to edit that.
Hope my answer helps you

How to define web.config local variables

I have a web application in C# with .NET Framework 4.0 and I'm trying to find a way to define a variable in Web.Config that can be referenced elsewhere within the Web.Config.
I want something like this.
<?xml version="1.0" encoding="utf-8"?>
<LocalWebConfigVars>
<add key="Var1" value="ServerName1"/>
<add key="Var2" value="DatabaseName1"/>
</LocalWebConfigVars>
<configuration>
<connectionStrings>
<add name="AppConnectionString" connectionString="DATA SOURCE=#Var2 ServerName=#Var1"/>
<add name="OtherStuff" value="#Var1"/>
etc...
Currently I have to keep 3 or 4 hard-coded values (some embedded others just the value) updated to the same thing and would like to make it easier to keep in sync.
Is this possible?
Thanks.
Edit:
Just some background. The reason this is becoming problematic is that we define the apps database instance (among other instance specific setting) in the web.config. We have multiple database instances in our test and production environments and if I need to switch to a different one them while testing something and miss one of the hand full of references I get some strange results. I'm trying to avoid this by defining it once and referencing it everywhere else.
Would not doing a simply .config transformation for each of your environments work, by setting up a project configuration & transform, you would be able to swap from environment to environment by the use of solutions configuration dropdown.
See this link
for more info on transforms on .config files

How can I make a C# project's settings be user specific?

I am writing a Web API 2.0 project and a test project using Visual Studio 2013.
In the test project, I saved some information in the Settings.settings file (under TestProject->Properties in the Solution Explorer). One of the things saved there is the connection string to a database that is stored locally.
Unfortunately, the connection string will be slightly different on each person's computer when they download the repo. When people push their code to the master repo it overwrites the connection string, affecting everyone else.
What is the best way to make this configurable for each user such that everyone can have their own database path, but pushing to master repo won't affect anyone?
Edit
I don't think this is exactly a duplicate of that other question. Although, yes, my configuration settings are stored in app.config (since they happen to be application settings rather than user settings), following the solution in the other answer will lead me with the same problem. The app.config will contain configSource="otherconfig.config", and when people push that file to the master repo, it will still clobber other people's values. I need something that allows the custom configurations to be source-controlled without affecting the other users of the project.
Visual Studio handles this automatically for WEB projects through Web.config transformations
You'll need to install a separate plugin for use with App.config and non-web projects. http://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859
The plugin basically adds the same functionality to app.config files, and works with the same syntax in the transform files.
Your best approach to this is to use Build Profiles. Have a developer-specific Web.developer.config and with that you get each user to choose their name in Configuration Manager. Then just make the new config, which is technically an XSLT make the changes needed for each team member.
Think of it as Debug vs Release configs, except in your case you'll have many Debug (one for each user). The Build profile you set doesn't get checked into TFS, so you're fine.
This is what a subconfig looks like:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
-->
<connectionStrings>
<add name="RavenDB" connectionString="Url=http://xxx/databases/xxx" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>
<appSettings>
<add key="BaseUrl" value="http://xxx" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
<system.net>
<defaultProxy enabled="true" />
<mailSettings>
<smtp xdt:TrandeliveryMethod="Network" transform="Replace">
<network xdt:Transform="Replace" host="xxx" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
</configuration>
More info on web.config transforms
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
The way I handle this problem is by adding a folder into my app that has only assets that don't get included in the build/publish. One of the things I include in that folder is DeveloperName.App.config files for each of my developers. Then I leave the the actual App.config file out of source control. When they check out the project, they copy their personalized DeveloperName.App.config file to the project folder and rename it to App.config.
This isn't perfect, but it gives you at least most of the goals you're looking for: The developers each get their own App.config file they can maintain and keep in source control. And the changes they make to App.config don't clobber each other every check-in.

How to encrypt config file ? desktop .NET

Please give me a working example.
I have already created a topic, but I'm not there who are not helped How to encrypt app.config?. Or am I just not what is not understood.
Once again, I beg you give 100% working example.
I need to encrypt connectString to access the DB mysql.
is an example http://www.c-sharpcorner.com/UploadFile/yougerthen/306132008064100AM/3.aspx.
There are two ways the first I have an error.
Second, I do not understand how to use
Do not write a lot of text, I do not understand very well. I'm Russian.
UPDATE:
I decided to use http://msdn.microsoft.com/en-us/library/system.configuration.rsaprotectedconfigurationprovider.aspx.
But I have an error: "Failed to encrypt the section 'connectionStrings' using provider 'RsaProtectedConfigurationProvider'. The error message from the provider: The object already exists."
I Create App WPF. Append file app.config.
app.config body:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="myconnect"
connectionString="myConnectString" />
</connectionStrings>
</configuration>
What do I do next ?
Do this in your installer,
rename app.config to web.config.
Use ASP.NET way to encrypt the data you want to encrypt.
Rename it back to what.exe.config.
Best practice is to do it manualy in your settings class: http://msdn.microsoft.com/en-us/library/system.configuration.rsaprotectedconfigurationprovider.aspx

How to make the Setting works for Class Library project in C# 2.0?

I am putting the setting under the property of one of my C# Class Library project for app setting:
EUCAccountService_ConnectionString
EUCTelcoDB_ConnectionString
In the development, it works nicely. Until I deported to production, I realise that the component that use those thing .. it just hang. I found that under \BIN when it compiled dewaCorp.EUC.TelcoDB.Data.dll.config and open up that file and turn out nothing.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
How to make this works? I thought by compiling it, it turned to some sort config file or something. But it didn't.
I am appreciated your comment.
The properties are not stored in the .config file they are stored in the windows user profiles.
To store setting in the .config file add a config file to the executing assembly (take note is important to use the executing assembly) and store add the settings there for connection strings there is a special note for them.
<ConnectionStrings>
<ConnectionString />
</ConnectionStrings>
You'd better take a look at similar projects, such as log4net, and Enterprise Library.
http://logging.apache.org/log4net/index.html
http://www.codeplex.com/entlib

Categories

Resources