开发者

Accessing another project's settings file

开发者 https://www.devze.com 2022-12-25 07:51 出处:网络
Is there a way to access the settings file from a different project? For example, I have a solution that contains 2 projects (Lets call them Proj1 and Proj2). I want to access the application settings

Is there a way to access the settings file from a different project? For example, I have a solution that contains 2 projects (Lets call them Proj1 and Proj2). I want to access the application settings of Proj2 from Prog开发者_如何学运维ram.cs in Proj1. Is this possible?


The answer if you are using C#:
The very simple answer is to right click on proj2, choose the settings tab. On the top, you will find the access modifier of the settings class is: internal, change it to public. Add a reference to the proj2 in the proj1 in order to see the proj2 Settings class. That's all.


Option A : parse the values out of the other assembly's configuration file (where the settings are stored)

Option B : create a public class in Proj2 that exposes the necessary values from its settings as static properties, then reference the assembly in Proj1 and consume the values from that class.

Option C : If you want to expose ALL the settings, you can modify the access of the settings class from internal to public.

I'm sure there other ways as well.


I'll repost the contents of @Kildareflare's link for future reference. Still works in VS2015, but for myself I think I prefer "Option B" above.

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 there is a problem the way it's implemented. 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 being 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.

Accessing another project's settings file

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

Accessing another project's settings 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:

Accessing another project's settings file

Notice that I added a folder with the same name as my Engine project. This will be helpful 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";
        }
    }
}


ConfigurationManager has that covered:

string proj2Exe = @"C:\projects\proj2\bin\Debug\proj2.exe";
Configuration proj2Config = ConfigurationManager.OpenExeConfiguration(proj2Exe);
string mysetting = proj2Config .AppSettings.Settings["ThatSetting"].Value;


I had to come up with another solution besides the ones already given here because I was using XML Tranforms (via SlowCheetah) on the App.config of my project containing the settings. If you are not doing this, I recommend one of the other solutions.

I added an after-build step in the consuming project (Proj1 in the example) to copy the config file from the output folder of Proj2. This will ensure that the config will have the transforms applied. (In my case, Proj1 is a dll, so if yours is an exe, change the DestinationFiles from ".dll.config" to ".exe.config".) Snippet from Proj1.csproj:

<Target Name="AfterBuild">
  <Copy SourceFiles="..\Proj2\bin\$(Configuration)\Proj2.exe.config" DestinationFiles="$(TargetDir)\$(AssemblyName).dll.config" />
</Target>

Then I created a link of the Settings.settings from Proj2 by ctrl+shift+dragging the file to Proj1 (as in the blog article referenced by Kildareflare).

I could then reference settings in Proj1 similar to: Proj2.Properties.Settings.Default.MySetting.

Note: If you are doing this for unit tests like I am (Proj1 is a test DLL), and you are using the ReSharper test runner, be sure to configure it to run tests in separate AppDomains.


Faced with this trouble today, mate. Solved by adding the settings section of the second project between configSections of the first project's app.config file

<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxx">
      <section name="fullSecondProjectName" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxx" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>

and then don't forget to add those user settings

<configuration>

  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxx">
      <section name="fullSecondProjectName" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxx" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>

  ...

  <userSettings>
    <fullSecondProjectName>
      <setting name="LogMethodInvocation" serializeAs="String">
        <value>True</value>
      </setting>
    </fullSecondProjectName>
  </userSettings>

</configuration>


Since Settings.Designer.cs is an internal class, and you don't want to mess with a generated code file, I would recommend to add the secondary as a "friend" project.

From: C# "internal" access modifier when doing unit testing

Add following code to the Proj2's AssemblyInfo.cs

using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("Proj1")]


I've not tested this method myself but Eric De Carufel's little trick may be what you need:

http://blog.decarufel.net/2007/10/getting-access-to-settings-in-another.html

The original link appears to be dead as he has moved to a new blog and deleted old content.

The original content is below:

Getting access to Settings in another project

Thursday, October 25, 2007

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";
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号