0

我在 C# 中有 2 个控制台应用程序:App1App2,设置属性访问权限设置为公共,我遵循一些说明:

1.- 我从 App1 设置属性设置 App2 'Name'

App2.Properties.Settings.Default.Name = "John"

2.- 我运行 App1 并按预期打印我们的 App2.Name 属性设置。

App2.Properties.Settings.Default.Name

3.- 我运行 App2,以测试是否从 App1 保存了属性。它不显示属性值:John

Properties.Settings.Default.Name

问题:

  • 为什么属性正确保存并显示在 App1 范围内,但是当我运行 App2(已声明属性名称)时却没有?

  • 如果我的方法无效;如何从外部控制台应用程序设置设置属性?

4

1 回答 1

0

Instead of referencing 2 console app, you could also use some XML operations:

using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
        var file = @"C:\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe.config";

        var doc = XDocument.Load(file);

        var desc = doc.Root.Descendants("setting")
            .Where(s => s.Attributes()
                .Any(y => y.Value == "Name")).Descendants("value");

        desc.First().Value = "NewName";

        doc.Save(file);
    }
}

}

In this snippet the path to the config file is hard wired. You could also use the registry, another file where App2 says where it's settings file is or anything else to share a string.

于 2014-01-16T16:37:06.983 回答