Settings value empty when main program not running

120 views Asked by At

I have put my program settings values in a separate project called Common in my solution. I have a main project (let us call it A), and some other projects (B and C) and then one Common project. Common is added to A, B and C's references. I have tried accessing Common's settings value by using both:

  1. Visual studio's Settings.Setting which is easy to use but tricky when it comes to reading from other projects.
  2. A self-made ini file to read from and write to.

And now I realized they are both acting the same way and I think I am missing an important point. The setting values are accessible when the main project is running. When I update my settings, they are saved and it all works fine.

But sometimes I need to access these values from other projects while A is not running. In my case, project B is triggered by a Windows Service. It then reads the data from database and then executes the batch file. But it also needs to access Common's settings. And they are empty strings!

So if we consider the 2nd approach above (reading from ini) Common has a value in its Conf static object (that I use to read and write the settings) called ConnectionString. This value is accessed in the main program by simply calling Common.Conf.ConnectionString. But when used by B, this value is empty. So I cannot access my Settings values.

What I might as well do, is to create a function that reads from the ini file every time I need these values and parse them once again as if the Common project does not exist.

But is there anything else I can do to make it work?

This is the code to my Common's Conf sealed class that I use to read/write from the ini file.

namespace Common
{
    public sealed class Conf
    {
        public static string ConnectionString { get; set; }
        public static bool LoggerEnabled { get; set; }
        public static string FolderLocation { get; set; }
        public static string Delimeter { get; set; }

        static readonly string SETTINGS = "conf.ini";
        static readonly Conf instance = new Conf();
        Conf() { }
        static Conf()
        {
            string property = "";
            string[] settings = File.ReadAllLines(SETTINGS);
            foreach (string s in settings)
                try
                {
                    string[] split = s.Split(new char[] { ':' }, 2);
                    if (split.Length != 2)
                        continue;
                    property = split[0].Trim();
                    string value = split[1].Trim();
                    PropertyInfo propInfo = instance.GetType().GetProperty(property);
                    switch (propInfo.PropertyType.Name)
                    {
                        case "Int32":
                            propInfo.SetValue(null, Convert.ToInt32(value), null);
                            break;
                        case "String":
                            propInfo.SetValue(null, value, null);
                            break;
                        case "Boolean":
                            if (value == "1")
                                propInfo.SetValue(null, true, null);
                            else
                                propInfo.SetValue(null, false, null);
                            break;
                    }
                }
                catch
                {
                    throw new Exception("Invalid setting '" + property + "'");
                }
        }

        /// <summary>
        /// Save new configuration in conf.ini
        /// </summary>
        public static void SaveConf()
        {
            StringBuilder sb = new StringBuilder();
            PropertyInfo[] properties = typeof(Conf).GetProperties();
            Type myType = typeof(Conf);
            PropertyInfo[] propertyinfos = myType.GetProperties(
                   BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
            foreach (PropertyInfo p in propertyinfos)
            {
                if (p.GetValue(myType, null) == null)
                    sb.AppendLine(string.Format("{0}:{1}", p.Name, string.Empty));
                else
                    sb.AppendLine(string.Format("{0}:{1}", p.Name, p.GetValue(myType, null).ToString()));

            }
            string result = sb.ToString();
            File.WriteAllText(SETTINGS, String.Empty);
            File.WriteAllText(SETTINGS, result);
        }        
    }
}
0

There are 0 answers