Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make Application.Properties.Settings public and stay that way

Tags:

I am in the process of centralizing my application settings into one location and I've chosen to use the the Settings collection in my common library to do so.

I have moved all these settings into their own file that gets pulled into my app.config using config source:

<Common.Properties.Settings configSource="config\Common.Properties.Settings.config" /> 

This allows me to use the "Add Link" capability of Visual Studio to override the default library settings with the imported config file within my web and test applications.

Now, I want to be able to access all of these great Settings values from within my other libraries, and have found that I can do so simply by making the generated class public:

File: Common.Properties.Settings

public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 

This lets me get access to things like Common.Properties.Settings.Default.MySetting from within my web application or unit tests. However, the problem is that whenever a new setting is added, the Setting.settings file is regenerated by Visual Studio, and flips the Settings class back to internal:

internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 

So my question is whether anyone knows of a way to override this, or perhaps suggest a macro approach or some other method to ensure that after the Settings.settings file is rebuilt, this class is set to public.

Thanks!

like image 880
Shane K Avatar asked Aug 08 '12 21:08

Shane K


People also ask

How can I save application settings in a Windows Forms application?

You can use the app. config file to save application-level settings (that are the same for each user who uses your application). I would store user-specific settings in an XML file, which would be saved in Isolated Storage or in the SpecialFolder.

What are application settings?

Application settings enable you to store application information dynamically. Settings allow you to store information on the client computer that shouldn't be included in the application code (for example a connection string), user preferences, and other information you need at runtime.

How add config file in C#?

In Visual Studio, from the Project menu, choose Add New Item. The Add New Item dialog box opens. In the Add New Item dialog box, select Settings File, enter a name for the file, and click Add to add a new settings file to your solution. In Solution Explorer, drag the new Settings file into the Properties folder.


1 Answers

I assume that you have created and modified settings form visual studio build in editor that can be accessed through project properties section settings. On the same editor there is an combobox that you need to change from internal to public it is marked as Access Modifier.

enter image description here

like image 84
Rafal Avatar answered Oct 13 '22 03:10

Rafal