Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Properties.Settings userSettings from a DLL

Tags:

c#

.net

A component of the GUI of my main application is its own project, and the main application just uses the .dll of the GUI component when running. Since the userSettings are stored in the GUI component's app.config, which the main app does not copy, I cannot save userSettings (no user.config created).

The possible ways around this I see are either being able to store the component's userSettings in an external config file (referenced by its app.config) that is copied to the main app's directory at runtime, or somehow putting all the userSettings at the main app's level (since its .exe.config is copied) and accessing it from the GUI component.

However, I have not been able to get either of these methods to work for saving the userSettings. Any ideas? If any clarification is needed, please let me know.

like image 267
Tara Avatar asked Jun 22 '11 20:06

Tara


People also ask

Can DLL have config file?

Using configuration files in DLL is not trivial, but is very simple. Using configuration files in DLL is not trivial. To accomplish this, we must follow these steps: Add a reference to System.

Where is settings file stored C#?

Settings. settings is located in the My Project folder for Visual Basic projects and in the Properties folder for Visual C# projects.

Where are C# properties stored?

It is a common requirement for many developers, and with C# it is really simple to do. The Microsoft.NET Framework 2.0 introduced a new item called the Settings File. This file is added to your project by default and can be found under the Properties folder within Visual Studio's Solution Explorer.

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.


2 Answers

You should be able to move the Settings from the main project Properties to the Properties of your GUI component DLL (or create a Settings.dll if you prefer).

Then reference this DLL from your main application and add using statements so you can access Settings.Default. You then have to change the access modifier of the settings file. This is a little non-obvious but explained in the MSDN documentation. Here's a screenshot:

enter image description here

Once you change this to 'public' you can just read the settings from anywhere across your application.

Warning: Don't have more than 1 Settings per running application because the implementation is a singleton and you'll get unexpected results. If you have one copy of Settings it doesn't need to be in the .exe dll - it can be wherever you want it to be.

I assume you're not trying to re-use across applications.

like image 99
Simon_Weaver Avatar answered Nov 15 '22 06:11

Simon_Weaver


Ah - the dreadful Properties.Settings!

There are several ways round this but all involve some rework.

Simplest - use appsettings instead of ApplicationSettings: Copy the appsettings required into each application that uses it.

Pros: easy, Cons: Config settings everywhere!

Create a custom Config Section: Lots of articles on this - see http://joelabrahamsson.com/entry/creating-a-custom-configuration-section-in-net for example.

Basically you'd then have your separate config file that you'd copy into each deployable that required it - simply reference the assembly where you create your Configuration Objects in each deployable that needs it, and update the application config file to reference the included custom config file.

Pros: Tidy, you can wrap validation inside the Configuration Object, Tidier config files, Cons: Takes a bit longer to set up.

There are others - including storing values in an XML file and deserializing it to a class at runtime. Using the same approach but storing in a database instead. etc. etc.

like image 39
BonyT Avatar answered Nov 15 '22 07:11

BonyT