简体   繁体   中英

Read user (not app) settings from another accessible C# app

Say I have two apps A and B. App A has standard Settings.settings type of configuration. I have made A's settings public (instead of default internal ). Referencing A in B and going for the A.Settings.Default yields only the design-time default values. There are no saved user settings. Invoking Reload has no effect.

Even simpler, importing a reference to A in LINQPad and dumping A.Settings.Default only shows default values. So effectively same as above.

I have found several similar questions even here on SO but none were with definite answers. Is there a simple way to do what seems to be a trivial task?

Let's assume your apps are A.exe and B.exe.

B.exe references A.exe.

When I run A.exe, it reads / writes the user settings to / from:

C:\Users\<user>\AppData\Local\A\A.exe_Url_ify12bcwso2vevlc4wawlllokphlsntr\1.0.0.0\user.config

When I run B.exe, it reads / writes the user settings (of A.exe) to / from:

C:\Users\<user>\AppData\Local\B\B.exe_Url_vhnd4ku3yt2pxsldv3qsblquadzyhg3g\1.0.0.0\user.config

I got these paths by monitoring disk activity of A and B exes with procmon.exe

As you can see, the logic behind the user settings is actually the same as app settings. The runtime is using the currently executing process's name to store the user settings, therefore it is not possible to access A.exe's settings from B.exe as they were saved from A.exe.

Here is the code I tested:

A.exe:

static void Main(string[] args)
{
    Console.WriteLine("A.Settings1.Default.AExeSetting1: {0}", A.Settings1.Default.AExeSetting1);
    A.Settings1.Default.AExeSetting1 += "-";
    Console.ReadLine();
    A.Settings1.Default.Save();
}

B.exe:

static void Main(string[] args)
{
    Console.WriteLine("A.Settings1.Default.AExeSetting1: {0}", A.Settings1.Default.AExeSetting1);
    A.Settings1.Default.AExeSetting1 += "-";
    Console.ReadLine();
    A.Settings1.Default.Save();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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