T.C.
T.C.

Reputation: 215

How Do I Reference the Application Settings of One Project from Another?

In a VB.Net project, you can use the Settings tab of the properties page to define application settings. To reference the settings in code, you use the syntax My.Settings.SettingName in VB.

On the Settings tab, you get to choose the Access Modifier. It may be "Friend" or "Public". Presumably, when you choose "Public", you are making the settings accessible to other assemblies. However, once "Public" is chosen, I can't figure out the syntax to reference the settings of one project from another. In fact, I can't observe any difference between using "Internal" vs. "Public" as the access modifier.

My question: Does choosing "Public" as the access modifier make settings accessible to other assemblies? If so, what is the syntax to reference the settings from other assemblies? If not, what does "Public" do?

Upvotes: 9

Views: 11582

Answers (5)

SteveCinq
SteveCinq

Reputation: 1963

This is a bit trickier in VB.NET than in C#. This is mainly because in VB.NET, name-spacing isn't such a big focus. So within a VB.NET solution, it's not uncommon - though possibly not great practice - to have the same root (aka default) namespace for all projects/sub-assemblies in a solution, eg:

MyNamespace.Main, MyNamespace.ClassLib1, MyNamespace.ClassLib2, MyNamespace.Common, etc.

Because of this, the inter-project settings access method described elsewhere in this question falls over.

The suggested way to access settings in a referenced project in VB.NET is:

  1. Make the Settings access modifier Public,
  2. Access the setting (read-only) with {MyNamespace}.My.MySettings.Default.{MySetting}.

Note that My.MySettings is a property of MyNamespace directly, not of the assembly name.

Only if the sub-assembly's namespace is different to the referencing project's namespace, will you have access to MySetting. If the namespaces are the same, it's simply not visible and I can't find any way to get to it. (I could dig into the ILM and maybe find out what the compiler is doing to avoid a conflict - which is what would probably otherwise be the case - but it's a moot point.)

I've tested this, in both C#.NET and VB.NET. Differentiate your root namespaces - if possible - and you will have joy.

Upvotes: 1

knaki02
knaki02

Reputation: 183

I was looking for the same thing, so this it :
In C# :
[Namespace].Properties.Settings.Default.[SettingName]
In VB :
[Namespace].My.MySettings.Default.[SettingName]

Upvotes: 3

Alexandr
Alexandr

Reputation: 1

Here is sample code to get value of setting named DataBaseName of any .exe located near our executing application and store it in "dbName" variable:

string currentDirectory = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string exeName = System.IO.Path.GetFileName(Assembly.GetExecutingAssembly().Location);
FileInfo[] fileInfos = new DirectoryInfo(currentDirectory).GetFiles("*.exe");
foreach (FileInfo fi in fileInfos)
{
    if (fi.FullName == System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) continue;

    Assembly asm = Assembly.LoadFrom(fi.FullName);
    Type[] allTypes = asm.GetTypes();
    foreach (Type type in allTypes)
    {
        if (type.Name != "Settings") continue;
        Type settingsType = type;

        PropertyInfo propDefault = type.GetProperty("Default");
        object defaultSettings = propDefault.GetValue(null, null);
        PropertyInfo piDBName = settingsType.GetProperty("DataBaseName");
        string dbName = (string)piDBName.GetValue(defaultSettings, null);

        break;
    }

    break;
}

Upvotes: 0

MikeG
MikeG

Reputation: 1069

I think you can pass an instance of your settings object from one DLL into another DLL (say at app startup) and then access the setting using the Item property of this class (Setting classes are class inherited from ApplicationSettingsBase)

'In DLL1: Class1 Shared Property SettingsFromAnotherDLL As ApplicationSettingsBase ...

'In DLL2: (done at startup) Class1.SettingsFromAnotherDLL = My.Settings.Default

'In DLL2 when accessing settings Dim Setting As STring = Class1.SettingsFromAnotherDLL.Item("SettingKey")

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941317

You are mixing things up rather badly. A setting doesn't have a accessibility modifier, they are always public. However, in a Winforms app, you indeed have a "Application settings" property in the Properties window for a control. Right at the top. And a Modifier property. The latter is Friend by default in a VB.NET project, Private in a C# app. That determines the accessibility of the control variable, not the setting.

Yes, My.Settings gives you access to the setting that stores the control property value. But that's where the good news ends. You should always set the Scope of the setting in the settings designer to User. So that the value can be saved and restored when your program starts back up.

Settings with User scope are stored in a file that's hard to find back. The typical path of such a file is C:\Users\hpassant\AppData\Local\WindowsFormsApplication1\WindowsFormsApplication1._Url_2acx4ldi2zmg42elj3eyqq0snhqco4qe\1.0.0.0

The first part is me, the current user of my laptop. The bizarro part of the path name is a hash, a value that's unique for the application name and version. And probably something else like the phase of the moon when I compiled the app. The algorithm to compute that hash isn't documented. Just that it will be unique to my app and cannot be stomped on by another app.

And that's the rub, one app cannot find the user scoped settings for another app. You'll have to give up on using settings if that's important to you. And replace it with, say, an XmlDocument in a well known location.

Upvotes: 6

Related Questions