public static void GetConfigurationUrl()
        {
            Globals.log.Debug("Configuration:> running GetConfigurationUrl()");

            configUrl = "";

            try
            {
                Globals.log.Debug("Trying to get ConfigurationURL in the registry " + Configuration.reg4ConfigurationURL + "/" + Configuration.regkey4ConfigurationURL);
                configUrl = (string)Registry.GetValue(Configuration.reg4ConfigurationURL, Configuration.regkey4ConfigurationURL, null);
            }
            catch (Exception e)
            {
                Globals.log.Warn("Can NOT get value from Registry");
                Globals.log.Warn(e.ToString());
            }

            if (String.IsNullOrEmpty(configUrl))
            {
                try
                {
                    Globals.log.Debug("ConfigurationURL is String.Empty . Trying to get the value by windows64bit method...");
                    configUrl = RegistryWOW6432.GetRegKey64(RegHive.HKEY_LOCAL_MACHINE, Configuration.reg4ConfigurationURL.Replace(@"HKEY_LOCAL_MACHINE\", ""), Configuration.regkey4ConfigurationURL);

                    if (String.IsNullOrEmpty(configUrl))
                    {
                        configUrl = RegistryWOW6432.GetRegKey32(RegHive.HKEY_LOCAL_MACHINE, Configuration.reg4ConfigurationURL.Replace(@"HKEY_LOCAL_MACHINE\", ""), Configuration.regkey4ConfigurationURL);
                    }
                }
                catch (Exception e)
                {
                    Globals.log.Debug("Error in getting the value by windows64bit method ");
                    Globals.log.Error(e.ToString());
                }

                if (String.IsNullOrEmpty(configUrl))
                {
                    Globals.log.Debug("ConfigurationURL is still String.Empty");
                }
            }
        }
Beispiel #2
0
        private void Configuration_SetFile()
        {
            try
            {
                Globals.log.Debug("Set configuration....");
                //---------- Download config file
                string str_content = "";

                string path4configReal = "";

                // prepare path for (eg C:\Users\UserName\AppData\Local\AmivoiceWatcher\configuration.txt)
                //string path4config = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                //path4config = Path.Combine(path4config, "AmivoiceWatcher");



                string path4config = Path.Combine(Globals.PathLocalAppData, @"configuration.txt");

                Globals.Initilize();
                // get url from registry
                // then load file content
                Globals.log.Debug("Setup configuration value ...");
                string ConfigurationURL = "";
                try
                {
                    Globals.log.Debug("Trying to get ConfigurationURL in the registry " + Globals.reg4ConfigurationURL + "/" + Globals.regkey4ConfigurationURL);
                    ConfigurationURL = (string)Registry.GetValue(Globals.reg4ConfigurationURL, Globals.regkey4ConfigurationURL, null);
                }
                catch (Exception e)
                {
                    Globals.log.Warn("Can NOT get value from Registry");
                    Globals.log.Warn(e.ToString());
                }

                if (String.IsNullOrEmpty(ConfigurationURL))
                {
                    try
                    {
                        Globals.log.Debug("ConfigurationURL is String.Empty . Trying to get the value by windows64bit method...");
                        ConfigurationURL = RegistryWOW6432.GetRegKey64(RegHive.HKEY_LOCAL_MACHINE, Globals.reg4ConfigurationURL.Replace(@"HKEY_LOCAL_MACHINE\", ""), Globals.regkey4ConfigurationURL);

                        if (String.IsNullOrEmpty(ConfigurationURL))
                        {
                            ConfigurationURL = RegistryWOW6432.GetRegKey32(RegHive.HKEY_LOCAL_MACHINE, Globals.reg4ConfigurationURL.Replace(@"HKEY_LOCAL_MACHINE\", ""), Globals.regkey4ConfigurationURL);
                        }
                    }
                    catch (Exception e)
                    {
                        Globals.log.Debug("Error in getting the value by windows64bit method ");
                        Globals.log.Error(e.ToString());
                    }


                    if (String.IsNullOrEmpty(ConfigurationURL))
                    {
                        Globals.log.Debug("ConfigurationURL is still String.Empty");
                    }
                }

                if (!String.IsNullOrEmpty(ConfigurationURL))
                {
                    Globals.log.Debug("Start downloading config file from ConfigurationURL(registry) = " + ConfigurationURL);
                    try
                    {
                        HttpWebRequest  webRequest = (HttpWebRequest)HttpWebRequest.Create(ConfigurationURL);
                        HttpWebResponse webResp    = (HttpWebResponse)webRequest.GetResponse();

                        if (webResp == null || webResp.StatusCode != HttpStatusCode.OK)
                        {
                            //if error str_content = "", so move code to the end of this method
                        }
                        else
                        {
                            using (Stream stream = webResp.GetResponseStream())
                                using (StreamReader reader = new StreamReader(stream))
                                {
                                    str_content = reader.ReadToEnd();
                                }



                            // Append text to an existing file named "WriteLines.txt".
                            //using (StreamWriter outputFile = new StreamWriter(path4config, true))
                            //{
                            //    outputFile.WriteLine(str_content);
                            //}

                            System.IO.File.WriteAllText(path4config, str_content);


                            Globals.log.Debug("Complete uploading file to " + path4config);

                            //Console.WriteLine(path4config);


                            //log.Debug("Load file from internet completed");
                        }
                    }
                    catch (Exception e)
                    {
                        Globals.log.Warn("Can not load config file from server! The recorded one(if exists) will be used.");
                        Globals.log.Warn(e.ToString());
                    }
                }


                //---------- Set which config. file will be used.
                if (File.Exists(path4config))
                {
                    path4configReal = path4config;
                }
                else
                {
                    //To get the location the assembly normally resides on disk or the install directory
                    //string path4configSameAsExe = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                    string path4configSameAsExe = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    path4configSameAsExe = Path.Combine(path4configSameAsExe, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".ini");

                    if (File.Exists(path4configSameAsExe))
                    {
                        path4configReal = path4configSameAsExe;
                    }
                }


                if (String.IsNullOrEmpty(path4configReal))
                {
                    Globals.log.Warn("There is no configuration file! The default code-embed one will be used.");
                }
                else
                {
                    Globals.log.Debug("Setup configuration from file: " + path4configReal);
                    try
                    {
                        Globals.iniData = Globals.iniParser.ReadFile(path4configReal);


                        foreach (IniParser.Model.KeyData key in Globals.iniData.Sections.GetSectionData("watcher").Keys)
                        {
                            Globals.configuration[key.KeyName] = key.Value;
                        }
                    }
                    catch (Exception e)
                    {
                        Globals.log.Warn(String.Format("Can not parse ini file: {0}", e.ToString()));
                    }
                }



                //For debug
                foreach (string key in Globals.configuration.Keys)
                {
                    Globals.log.Debug(String.Format("Globals.configuration[{0}]={1}", key, Globals.configuration[key]));
                }

                //Post set configuration value
                Globals.Configuration_SetValueFromSetFile();

                Globals.log.Debug("Set configuration....OK");
            }
            catch (Exception e)
            {
                Globals.log.Error(e.ToString());
            }
        }