0

I have 2 Console Applications in C#: App1 and App2 with Setting Properties access set to public and I follow some instructions:

1.- I set Property setting App2 'Name' from App1

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

2.- I run App1 and it prints our App2.Name Property Setting as expected.

App2.Properties.Settings.Default.Name

3.- I run App2, to test if property is saved from App1. It does not show the property value: John

Properties.Settings.Default.Name

Question:

  • Why property is saved correcty and shown in App1 scope, but when I run App2 (which has declared the property Name) does not?

  • If my approach is not valid; HOw can I set Setting Properties from an external Console Application?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Daniel Camacho
  • 423
  • 5
  • 12
  • 27
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 16 '14 at 14:27
  • Each app has it's only config file where they load their settings from. Just setting it in one isn't going to set it in the other. They are several ways around this, it is possible to load a config file manually and change it, or you could look at any number of other ways to share data between two apps. – Matt Burland Jan 16 '14 at 14:46

1 Answers1

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.

csteinmueller
  • 2,427
  • 1
  • 21
  • 32
  • In ConsoleApplication2.exe.config the value of the tag "value" is empty after saving. " – Daniel Camacho Jan 17 '14 at 07:53
  • The .exe.config file gets deployed, depending on the contant of the .settings file. If you compile App2 after you did the manipulation within App1, the changes get overridden. You can compile both, run App1, then run App2 directly from the .exe. In this case the changes doesn't get overridden. What's your goal? Maybe there are other ways to do it. – csteinmueller Jan 17 '14 at 09:33