Sharing config/settings between projects in a solution.

dean

Member
Joined
Jan 22, 2006
Messages
9
Programming Experience
3-5
I need to share some user settings between projects in a solution, does anyone know how this is best achieved?

Also what's the best way of sharing common resources (strings/images) between projects?

Thanks in advance.
 
To share settings between projects you could save the settings in a file and reference that file in the different projects. I would suggest a XML based settings file, there are many examples of classes that can save settings in xml format. I would also suggest putting the xml settings class in a dll so you can reference it in all projects that need to use it.

To share resources between projects: create a new Class Library project to the solution, add a resource file, add the images/resources to the resource file, then create a class with public shared properties that retrieve the resources. You can then add a reference to the compiled dll in each project that needs to access the resources.
 
You have .NET 2
Settings should go in the Settings section of a project's properties

To use the settings in another namespace you need to hack it a bit.

Show all files in the project menu
Then expand the My Project node
Expand Settings.settings
Open the Settings.Designer.vb file
Change the main class in there from Friend access level to Public access level

You can now say in WindowsApplication1:


Global.WindowsApplication2.My.MySettings.Default.SettingName



Note that this is slightly different to what youll do in WinApp1:

Global.WindowsApplication1.My.Settings.SettingName


But its the same end, just a different route
 
note the comment in the Designer file:
VB.NET:
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:2.0.50727.832
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
 
You should use a class library to share resources. There are several options for this including embedding files, the easiest is to use the My.Resources feature (project settings, resources tab). The itch about my.resources is it creates a strongly typed proxy class but with Friend access so it isn't available across assemblies. You can still attach a ResourceManager to read any My.resource from other assemblies, see the ResourceManager property in the generated class for a code sample. With just a little tweak you can generate a strongly typed Public class for use by other assemblies and it will work just like the My.Resources object. The resources stored by My.Resources is in path "My Project\Resources.resx", run ResGen.exe in that folder with this command:
resgen Resources.resx /publicClass /str:vb,ClassLibrary1,Resources,res.vb
You will get a warning about the naming of output file "Resources.resources" (not specified, and not used), just ignore it. Now "add existing" in class library project to add the generated "res.vb" file. "ClassLibrary1" is the namespace of the class library project, make sure you match the real namespace.

If you have a string resource String1 you would with My.Resources use this code in the local assembly:
VB.NET:
Label1.Text = My.Resources.String1
To get access through the newly generated public proxy to the class library you use this code, the consumer of the class library have it referenced, in this example the namespace is "ClassLibrary1":
VB.NET:
Label1.Text = ClassLibrary1.ClassLibrary1.Resources.String1
 
Many thanks for all the help. :)

Is it possible to create a property that exposes My.Mysettings.Default as Settings so that instead of ClassLibrary1.My.MySettings.Default.TestSetting I could have:

VB.NET:
ClassLibrary1.Settings.TestSetting

At the moment I've got this property in another class so it's ClassLibrary1.Configuration.Settings.TestSetting, but I'd like to drop the Configuration class if possible.

Thanks in advance.
 
Er..
VB.NET:
Class ClassLibrary1

  Public ReadOnly Property Settings
    Get
      Return Me.My.MySettings.Default
    End Get
  End Property

WHy you actually care about the extra word, when you have intellisense, I'm not really sure..
 
Two reasons:

1. I've got team members who will care.
2. I'm curious as to whether it's possible to do it.
 
Two reasons:

1. I've got team members who will care.
I'd ask them if they know how a light switch works. If they dont, then point out to them that some things are just the way they are, and they dont have a problem using them without question.. If they know how a light switch works, repeat the logic for a fridge, car, ball point pen, dishwasher soap tablet..

2. I'm curious as to whether it's possible to do it.
It's possible to do anything. A better question to ask is, if it's worth doing it..
 
Note also that these suggestions are workarounds/hacks/tweaks to get access to the My feature across assemblies. The My object is primarily designed for local assembly where you simply type My.Settings... and My.Resources... etc.

The generated public Resources proxy class that hooks to My.Resources as described in earlier post can also be modified further, you can for example simply comment out the namespace in that class, the class library already has that namespace but the code generator needed that info. In that case you will narrow down the access to f.ex:
VB.NET:
Label1.Text = ClassLibrary1.Resources.String1
It doesn't get much simpler than this, writing theAssembly.Resources instead of My.Resources.

There are also other options like creating your own settings or resource storage and generating or writing specialized strongly typed accessor classes. Also probably much more work, but you should look into other possible solutions as well.
 
Back
Top