0

I have 2 views (console and wpf) and 1 dll with program logic. In 1 wpf I want to set some user settings and then use them in console even after the wpf is closed. But the settings always reset after closing the wpf. I am trying to save a list of objects, so that's why the code is also using MemoryStream, BinaryFormatter, etc. but I think it makes no difference in the functionality of the ConfigurationManager.

This is example of my code:

    public List<CsprojFile> FilesArray
    {
        get
        {
            try
            {
                using (
                    MemoryStream memoryStreams =
                        new MemoryStream(Convert.FromBase64String(ConfigurationManager.AppSettings["filesArray"])))
                {

                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    return (List<CsprojFile>) binaryFormatter.Deserialize(memoryStreams);
                }

            }
            catch
            {
                return new List<CsprojFile>();
            }
        }
        set
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, value);

                memoryStream.Position = 0;
                byte[] buffer = new byte[(int)memoryStream.Length];
                memoryStream.Read(buffer, 0, buffer.Length);

                ConfigurationManager.AppSettings["filesArray"] = Convert.ToBase64String(buffer);
            }
        }
    }
Tomáš Zajda
  • 147
  • 1
  • 15
  • 2
    If you want to use configuration settings across two applications (WPF and Console) then you need to store them somewhere accessible by both. The most obvious choice would be a database. If you don't already use a database in your applications maybe write to a file instead. ConfigurationManager is meant for configuration for that application only. – Andy Nichols Feb 03 '16 at 09:44
  • Maybe you can look at this post http://stackoverflow.com/questions/5274829/configurationmanager-appsettings-how-to-modify-and-save – Thomas Feb 03 '16 at 09:48
  • Sorry, forgot to write that only the dll is working with the ConfigurationManager. The views only ask it for the data. So isn't it possible to make it work even like this? Basically the views have no idea any settings file exist, they are supposed to just get it from the dll. – Tomáš Zajda Feb 03 '16 at 09:49
  • You could potentially store it in some sort of cache. But a persistent store seems to be what you want. – Jamie Rees Feb 03 '16 at 09:52
  • Does that mean I should just make some extern xml file? – Tomáš Zajda Feb 03 '16 at 09:56
  • Wouldn't the file containing AppSettings be locked untill application exit anyway? (I may be wrong, someone correct me, or is it not an issue when using ConfigurationManager? edit, ok, now I know how to do it: http://stackoverflow.com/a/4216827/891715). If your app is installed into ProgramFiles, there will also be folder permission issues when using Appplication scope settings? Wouldn't it be easier to just Seialize/Deserialize your data to a file and manage its location explicitly? – Arie Feb 03 '16 at 11:07

1 Answers1

0

ConfigurationManager reads from the applications App.Config, if you have two applications then you will be reading from two configurations as they are application specific.

If you want to have configuration that is shared between the applications you need to create some kind of persistent store that both applications access, a database, an XML file that you interpret or you can open a configuration file at a specific location with ConfigurationManager.

Another option is to put the setting you want in the machine config, which will then get read by both applications, so long as they run on the same machine, I wouldnt advise this though, only mentioned it for completeness.

Reading a configuration file from a known location with ConfigurationManager:

var config = ConfigurationManager.OpenExeConfiguration(@"\\file\path\here.someExtension");

Documentation: https://msdn.microsoft.com/en-us/library/ms224437(v=vs.110).aspx or another example: Loading custom configuration files

Community
  • 1
  • 1
David McLean
  • 1,462
  • 1
  • 12
  • 27