Thursday, October 25, 2007

Getting access to Settings in another project

One of the new cool features of Visual Studio 2005 is the new property editor. With this property editor you can easily add setting to your application. But ther is a problem the way its impelemented. Let me explain you why.

Usually the settings are specific to a project. When you add a setting in a project a special custom tool associate with the setting file generates a new class which you can use to access it. What is good about this class is it's strong typed. But behind the scene it's just getting a key from an xml file. This generated class is set as "internal sealed". This prevent from beeing accessed from any other assembly. What if you want to centralize where you edit these settings.

After many attempt to expose it I found a quick an easy way to do it. Let's say we have 2 project in our solution: an Engine and a WinApp. Each have settings but we want them to be editable from WinApp. Here is what it look like.




If you want to get access to Engine settings here th trick: Add a link file.

The link file will be compiled as part as you WinApp project. The setting class will still be internal and sealed but to WinApp project instead of Engine.

Here is the final result:


Notice that I addes a foler with the same name as my Engine project. This will be helpfull if you want to add settings from many projects.

With this in place you can access you engine setting the way from you engine class as from your WinApp class. You may omit the “Engine” part from your engine class because you should be in the same namespace. Here is what it should look like:

namespace WinApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void AccessConfig()
        {
            Engine.Properties.Settings.Default.EngineSetting = "test";
        }
    }
}

7 comments:

Unknown said...

Hi, I'm trying to do exaclty what you descibe.

After I add the link to the settings file, how can i access the "remote" settinds from a class in WinApp???

Eric De C# said...

I updated the post to show how it should look like.

Anonymous said...

Thanks for posting this article. This is exactly what I needed.
-swathi

Anonymous said...

My name is Wanda Lee I was browsing internet and found your blog. The author did a great job. I will subscribe to your RSS feeds. Thank you for your contribution. I am a web designer myself. And here some examples of the websites that I designed for payday loan online payday loan company.

Anonymous said...

OK, but what about user settings? This is fine for static or installed settings but user settings, apparently, are not shareable without a settings provider.

David said...

EXACTLY what I wanted--THANKS!

Glenn Rogers said...

Just what I wanted too! Works beautifully. Thanks!

Glenn
DBGallery Product Manager