访问另一个项目的设置文件

发布于 2024-08-27 03:28:21 字数 114 浏览 9 评论 0 原文

有没有办法从不同的项目访问设置文件?例如,我有一个包含 2 个项目的解决方案(我们称它们为 Proj1 和 Proj2)。我想从 Proj1 中的 Program.cs 访问 Proj2 的应用程序设置。这可能吗?

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 Program.cs in Proj1. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

心病无药医 2024-09-03 03:28:21

如果您使用 C#,答案是:
非常简单的答案是右键单击 proj2,选择“设置”选项卡。在顶部,你会发现设置类的访问修饰符是:internal,将其更改为public。在 proj1 中添加对 proj2 的引用,以便查看 proj2 Settings 类。就这样。

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.

青朷 2024-09-03 03:28:21

选项 A:从其他程序集的配置文件(存储设置的位置)中解析值

选项 B:在 Proj2 中创建一个公共类,将其设置中的必要值公开为静态属性,然后引用Proj1 中的程序集并使用该类中的值。

选项 C :如果您想公开所有设置,您可以将设置类的访问权限从 internal 修改为 public

我确信还有其他方法。

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.

情未る 2024-09-03 03:28:21

我将重新发布@Kildareflare 链接的内容以供将来参考。在 VS2015 中仍然有效,但就我自己而言,我认为我更喜欢上面的“选项 B”。

访问另一个项目中的设置

Visual Studio 2005 的一项很酷的新功能是新的属性编辑器。使用此属性编辑器,您可以轻松地将设置添加到您的应用程序中。但其实施方式存在问题。让我解释一下原因。

通常这些设置特定于项目。当您在项目中添加设置时,与设置文件关联的特殊自定义工具会生成一个可用于访问它的新类。这个类的优点是它是强类型的。但在幕后,它只是从 xml 文件中获取密钥。该生成的类被设置为“内部密封”。这可以防止从任何其他程序集访问。如果您想集中编辑这些设置,该怎么办?

经过多次尝试揭露它后,我找到了一种快速简单的方法来做到这一点。假设我们的解决方案中有 2 个项目:一个 Engine 和一个 WinApp。每个都有设置,但我们希望它们可以从 WinApp 进行编辑。这是它的样子。

之前的设置

如果您想访问引擎设置,请使用以下技巧:添加链接文件。

添加链接

链接文件将作为 WinApp 项目的一部分进行编译。设置类仍然是内部和密封的,但是是 WinApp 项目而不是 Engine。

这是最终结果:

Settings After

请注意,我添加了一个与我的 Engine 项目同名的文件夹。如果您想从许多项目添加设置,这将很有帮助。

完成此操作后,您可以像从 WinApp 类一样从引擎类访问引擎设置。您可以从引擎类中省略“Engine”部分,因为您应该位于相同的命名空间中。它应该是这样的:

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

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

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.

Settings Before

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

Add Link

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:

Settings After

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";
        }
    }
}
不可一世的女人 2024-09-03 03:28:21

ConfigurationManager 涵盖了:

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

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;
笑梦风尘 2024-09-03 03:28:21

除了这里已经给出的解决方案之外,我还必须想出另一个解决方案,因为我在包含设置的项目的 App.config 上使用 XML 转换(通过 SlowCheetah)。如果您不这样做,我推荐其他解决方案之一。

我在使用项目(示例中的 Proj1)中添加了构建后步骤,以从 Proj2 的输出文件夹复制配置文件。这将确保配置将应用转换。 (在我的例子中,Proj1 是一个 dll,所以如果你的是一个 exe,请将 DestinationFiles 从“.dll.config”更改为“.exe.config”。)来自 Proj1.csproj 的片段:

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

然后我创建了“设置”的链接通过 ctrl+shift+将文件拖动到 Proj1 来从 Proj2 进行设置(如 Kildareflare 引用的博客文章中所示)。

然后,我可以引用 Proj1 中的设置,类似于:Proj2.Properties.Settings.Default.MySetting

注意:如果您像我一样针对单元测试执行此操作(Proj1 是一个测试 DLL),并且您正在使用 ReSharper 测试运行程序,请务必 将其配置为在单独的 AppDomain 中运行测试

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.

扮仙女 2024-09-03 03:28:21

我自己没有测试过这种方法,但 Eric De Carufel 的小技巧可能正是您所需要的:

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

出现原始链接他已经死了,因为他已经转移到新博客并删除了旧内容。

原始内容如下:

访问另一个项目中的设置

2007 年 10 月 25 日星期四

Visual Studio 2005 的一个新的很酷的功能是新的属性编辑器。使用此属性编辑器,您可以轻松地将设置添加到您的应用程序中。但其实施方式存在问题。让我解释一下原因。

通常这些设置特定于项目。当您在项目中添加设置时,与设置文件关联的特殊自定义工具会生成一个可用于访问它的新类。这个类的优点是它是强类型的。但在幕后,它只是从 xml 文件中获取密钥。该生成的类被设置为“内部密封”。这可以防止从任何其他程序集访问。如果您想集中编辑这些设置,该怎么办?

经过多次尝试揭露它后,我找到了一种快速简单的方法来做到这一点。假设我们的解决方案中有 2 个项目:一个 Engine 和一个 WinApp。每个都有设置,但我们希望它们可以从 WinApp 进行编辑。这是它的样子。

如果您想访问引擎设置,请使用以下技巧:添加链接文件。

链接文件将作为 WinApp 项目的一部分进行编译。设置类仍然是内部和密封的,但是是 WinApp 项目而不是 Engine。

这是最终结果:

请注意,我添加了一个与我的 Engine 项目同名的文件夹。如果您想从许多项目添加设置,这将很有帮助。

完成此操作后,您可以像从 WinApp 类一样从引擎类访问引擎设置。您可以从引擎类中省略“Engine”部分,因为您应该位于相同的命名空间中。它应该是这样的:

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

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

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";
        }
    }
}
或十年 2024-09-03 03:28:21

今天就遇到这个麻烦了,兄弟。通过在第一个项目的 app.config 文件的 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>

,然后不要忘记添加这些用户设置

<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>

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>
假装爱人 2024-09-03 03:28:21

由于 Settings.Designer.cs 是一个 internal 类,并且您不想弄乱生成的代码文件,因此我建议将辅助类添加为“朋友” “ 项目。

来自:进行单元测试时的 C#“内部”访问修饰符

将以下代码添加到 Proj2AssemblyInfo.cs

using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("Proj1")]

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")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文