Example #1
0
        /// <summary>
        /// Parse json object to AdConfiguration object
        /// </summary>
        /// <param name="reader"></param>
        /// <returns>Newly parsed AdConfiguration</returns>
        private static AdConfiguration ParseConfiguration(JsonTextReader reader)
        {
            var newConfig = new AdConfiguration();

            while (reader.TokenType != JsonToken.EndObject)
            {
                if (reader.TokenType != JsonToken.PropertyName)
                {
                    reader.Read();
                    continue;
                }
                switch (reader.Value)
                {
                case "Name":
                    reader.Read();
                    newConfig.ConfigName = reader.Value.ToString();
                    break;

                case "ComputerSearch":
                    reader.Read();
                    newConfig.ComputerSearch = reader.Value.ToString();
                    break;

                case "StaffUserSearch":
                    reader.Read();
                    newConfig.StaffUserSearch = reader.Value.ToString();
                    break;

                case "NonStaffUserSearch":
                    reader.Read();
                    newConfig.NonStaffUserSearch = reader.Value.ToString();
                    break;

                case "DefaultConnection":
                    reader.Read();
                    newConfig.DefaultConnection = reader.Value.ToString();
                    break;

                case "SitePrefix":
                    reader.Read();
                    newConfig.SitePrefix = reader.Value.ToString();
                    break;
                }
            }

            reader.Read();
            return(newConfig);
        }
Example #2
0
        /// <summary>
        /// Create a new AdConfiguration from the file at the specified path.
        /// </summary>
        /// <param name="path">Path to config file.</param>
        /// <returns>New AdConfiguration, empty if error reading file.</returns>
        private static AdConfiguration GetConfigFromFile(string path = "")
        {
            path = Path.Combine(Directory.GetCurrentDirectory(), path);
            if (!File.Exists(path))
            {
                return(new AdConfiguration());
            }

            var config = new AdConfiguration
            {
                FilePath = path
            };

            try
            {
                using (var reader = new JsonTextReader(new StreamReader(path)))
                {
                    while (reader.Read())
                    {
                        if (reader.Value != null)
                        {
                            if (reader.TokenType.ToString() == "PropertyName")
                            {
                                switch (reader.Value)
                                {
                                case "Name":
                                    reader.Read();
                                    config.ConfigName = reader.Value.ToString();
                                    break;

                                case "ComputerSearch":
                                    reader.Read();
                                    config.ComputerSearch = reader.Value.ToString();
                                    break;

                                case "StaffUserSearch":
                                    reader.Read();
                                    config.StaffUserSearch = reader.Value.ToString();
                                    break;

                                case "NonStaffUserSearch":
                                    reader.Read();
                                    config.NonStaffUserSearch = reader.Value.ToString();
                                    break;

                                case "DefaultConnection":
                                    reader.Read();
                                    config.DefaultConnection = reader.Value.ToString();
                                    break;

                                case "SitePrefix":
                                    reader.Read();
                                    config.SitePrefix = reader.Value.ToString();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (FileNotFoundException) { return(new AdConfiguration()); }

            return(config);
        }