简体   繁体   English

来自多个项目的 Visual Studio 用户 Properties.Settings.Default 文件?

[英]Visual Studio user Properties.Settings.Default file from multiple projects?

So, despite a lot of info out there being difficult to understand and find on the latest Visual Studio settings file, I got that figured out.因此,尽管在最新的 Visual Studio 设置文件中很难理解和找到很多信息,但我还是弄清楚了。 On your executable project, right-click and select properties , then left-click on the settings tab on the left, create a settings file (if needed), and fill in some settings names, types, and values in the table.在您的可执行项目上,右键单击并选择properties ,然后左键单击左侧的settings选项卡,创建一个设置文件(如果需要),并在表中填写一些设置名称、类型和值。 If you want these to be permanent defaults that are embedded in the executable and read-only, set the scope to "application", but if you leave it at the default scope of "user", then you can save new values to a special user.config file in the system AppData folder which will be read from every time your app is launched, overriding the embedded defaults.如果您希望这些是嵌入在可执行文件中且只读的永久默认值,请将范围设置为“应用程序”,但如果您将其保留在“用户”的默认范围内,那么您可以将新值保存到一个特殊的系统 AppData 文件夹中的 user.config 文件,每次启动应用程序时都会读取该文件,覆盖嵌入的默认值。 This basically allows every user to have a config file automatically stored by the system saving some config settings, such as last window location, etc.这基本上允许每个用户拥有系统自动存储的配置文件,保存一些配置设置,例如最后一个窗口位置等。

After setting up this, you can directly access the settings as follows:设置完成后,您可以直接访问设置,如下所示:

var variable1 = Properties.Settings.Default.Variable1;
var variable2 = Properties.Settings.Default.Variable2;

and so forth.等等。 To set the properties to a new value is equally easy:将属性设置为新值同样简单:

Properties.Settings.Default.Variable1 = variable1;
Properties.Settings.Default.Variable2 = variable2;

And then, if you want to save the special user file with the current values into something like C:\\users\\username\\AppData\\Local\\ApplicationName.exe.xxxx\\user.config , then just use:然后,如果要将具有当前值的特殊用户文件保存到C:\\users\\username\\AppData\\Local\\ApplicationName.exe.xxxx\\user.config ,则只需使用:

Properties.Settings.Default.Save();

Here is the issue - I'd like to access those same settings from all files in my solution, but the auto-generated methods are marked internal, so they can only be accessed from my main exe project.这是问题 - 我想从我的解决方案中的所有文件中访问这些相同的设置,但自动生成的方法被标记为内部,因此只能从我的主 exe 项目中访问它们。 I can create a class to essentially export the settings, but this only seems to work if the startup executable is always the same.我可以创建一个类来基本上导出设置,但这似乎只有在启动可执行文件始终相同时才有效。 When I run unit tests (with xunit 2.0), I find that the settings are not set - if I use the library to set them and save, they show up in a Microsoft folder in AppData , that appears to correspond with Visual Studio.当我运行单元测试(使用 xunit 2.0)时,我发现设置没有设置 - 如果我使用库来设置它们并保存,它们会显示在AppData的 Microsoft 文件夹中,这似乎与 Visual Studio 相对应。

Does anyone know a way I can reliably access the Properties.Settings.Default values of a project across an entire solution, or am I stuck going with a straight file at a known location and all the I/O and parsing that entails?有谁知道我可以在整个解决方案中可靠地访问项目的Properties.Settings.Default值的方法,或者我是否坚持在已知位置使用直接文件以及所有 I/O 和解析需要?

If so, is there a standard C# config file library that is used for this task outside of the built-in Visual Studio settings system?如果是这样,在内置的 Visual Studio 设置系统之外是否有用于此任务的标准 C# 配置文件库?


Edit - Update:编辑 - 更新:

The settings file appears to be based on the following:设置文件似乎基于以下内容:

  1. The Company Name in the Assembly Information (Properties/AssemblyInfo.cs or Project Properties/Application/Assembly Information/Company)装配信息中的公司名称(Properties/AssemblyInfo.cs 或 Project Properties/Application/Assembly Information/Company)

    • If the Company is empty, it appears to use the Default namespace from (/Application/Default namespace), which matches the namespace used in Properties/Settings.settings/Settings.Designer.cs如果 Company 为空,则它似乎使用来自 (/Application/Default namespace) 的 Default 命名空间,该命名空间与 Properties/Settings.settings/Settings.Designer.cs 中使用的命名空间相匹配
  2. The Executable Name (ie app.exe)可执行文件名称(即 app.exe)

    • This is strictly the executable name at runtime, including extension, so if you rename the file, that will change the settings file name and location in AppData这严格是运行时的可执行文件名,包括扩展名,因此如果重命名文件,将更改 AppData 中的设置文件名和位置
  3. The Assembly version汇编版本

The folder within AppData/Local will be #1 above and then a nested folder will have the executable name (#2 above) with some other characters appended, and then a final nested folder will be the version (#3 above), and the file inside that will be user.config. AppData/Local的文件夹将是上面的#1,然后嵌套文件夹将具有可执行文件名称(上面的#2)并附加一些其他字符,然后最后一个嵌套文件夹将是版本(上面的#3),并且里面的文件将是 user.config。

So, what appears to be happening is that when the unit test runner executes, the company of the runner is used for #1 (the built-in VS runners will use "Microsoft", and the Resharper runner will use "JetBrains", etc.), the exe name will still start off with the Namespace of the tests, and then the version number is also from the test runner (I think).因此,似乎正在发生的是,当单元测试运行器执行时,运行器的公司用于 #1(内置 VS 运行器将使用“Microsoft”,而 Resharper 运行器将使用“JetBrains”等.),exe 名称仍然会以测试的命名空间开头,然后版本号也来自测试运行程序(我认为)。

This means when running unit tests it will always look for and save to a different settings file than the main executable - so when you access the Properties.Settings.Default parameters from any assembly, it will look for the file in a different location (and save it to a different place) when running unit tests than from running the main executable.这意味着在运行单元测试时,它总是会寻找并保存到与主可执行文件不同的设置文件 - 因此,当您从任何程序集中访问 Properties.Settings.Default 参数时,它会在不同的位置(和将它保存到不同的地方)在运行单元测试而不是运行主可执行文件时。

You can always expose objects marked as internal by adding the following to the AssemblyInfo.cs [assembly: InternalsVisibleTo("UnitTestProject1")] in the main exe project.您始终可以通过将以下内容添加到主要 exe 项目中的 AssemblyInfo.cs [assembly: InternalsVisibleTo("UnitTestProject1")]来公开标记为内部的对象。 You should then have access to the internal objects from the test project.然后,您应该可以访问测试项目中的内部对象。

https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx

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

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