简体   繁体   English

整个解决方案的常见Settings.settings文件

[英]common Settings.settings file for entire solution

Is there a way to have common Properties file for all the projects of the solution? 有没有办法为解决方案的所有项目提供通用属性文件?

I have a console application and bunch of other class librarry files and i want to have only one Settings file for them. 我有一个控制台应用程序和一堆其他类库文件,我想只有一个设置文件。

I am not talking about app.Config file. 我不是在谈论app.Config文件。 I am talking about Settings.settings file. 我在谈论Settings.settings文件。

Any idea how i can do it? 知道我怎么做吗?

Also is it a good practice to store configuration settings in the Settings file rather than app.config? 将配置设置存储在设置文件而不是app.config中也是一个好习惯吗?

Place the file you want to share across projects in a Solution Folder. 将要共享的文件放在解决方案文件夹中的项目中。 Then, in each project, choose to Add Existing Item .. 然后,在每个项目中,选择Add Existing Item ..

In the Add Existing Item dialog, choose the common file. 在“添加现有项”对话框中,选择公用文件。 Instead of clicking the Add button, click the drop-down arrow on the right side of the Add button and choose Add As Link from the drop-down menu. 单击“添加”按钮右侧的下拉箭头,然后从下拉菜单中选择“添加为链接”,而不是单击“添加”按钮。

添加为链接

If this is for a Settings file, you need to set the Custom Tool property for the file: 如果这是设置文件,则需要为文件设置自定义工具属性:

文件属性

If your file is Settings.settings you can open your project properties and go to the Settings tab and Visual Studio will set this Custom Tool property for you automatically. 如果您的文件是Settings.settings ,则可以打开项目属性并转到“设置”选项卡,Visual Studio将自动为您设置此“自定义工具”属性。

Once the Custom Tool is set, you will see a Designer.cs file created underneath your settings file. 设置自定义工具后,您将看到在设置文件下创建的Designer.cs文件。 This Designer.cs file contains generated code to make your settings accessible programatically. 此Designer.cs文件包含生成的代码,以便以编程方式访问您的设置。

Designer子文件

As you can see, the Designer.cs file is also a linked file, and it's physical location will be the same as your settings file. 如您所见,Designer.cs文件也是一个链接文件,它的物理位置与您的设置文件相同。

Alternatively, you can set the Settings to public instead of internal . 或者,您可以将“ 设置”设置为“ public而不是“ internal

Go to YourProject->Properties->Settings 转到YourProject-> Properties-> Settings

在此输入图像描述

The answer accepted above by @quentin-starin is correct, but I had a situation where specifying the CustomToolNamespace came in handy as I already had some project specific settings. @ quentin-starin上面接受的答案是正确的,但我有一种情况,指定CustomToolNamespace派上用场,因为我已经有一些项目特定的设置。 By setting the Namespace to something specific, I did not get the autogenerated namespace relative to each project in the solution. 通过将Namespace设置为特定的值,我没有得到相对于解决方案中每个项目的自动生成的命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM