示例#1
0
        public Encoding GetEncoding(Stream inStream)
        {
            if (_encoding == null)
            {
                try
                {
                    ChoETLLog.Info("Determining file encoding...");
                    Encoding = ChoFile.GetEncodingFromStream(inStream);
                    ChoETLLog.Info("Found {0} encoding in file.".FormatString(Encoding));
                }
                catch (Exception ex)
                {
                    Encoding = _defaultEncoding;
                    ChoETLLog.Error("Error finding encoding in file. Default to UTF8.");
                    ChoETLLog.Error(ex.Message);
                }
                finally
                {
                    try
                    {
                        inStream.Position = 0;
                    }
                    catch { }
                }
            }

            return(Encoding);
        }
示例#2
0
        public static string GetValue(string key, string defaultValue = null, bool saveDefaultValue = false)
        {
            ChoGuard.ArgumentNotNullOrEmpty(key, "Key");

            if (Configuration == null)
            {
                return(defaultValue);
            }

            try
            {
                if (Configuration.AppSettings.Settings[key] == null)
                {
                    //_appConfig.AppSettings.Settings.Add(key, defaultValue == null ? String.Empty : defaultValue);
                    Configuration.AppSettings.Settings.Add(key, saveDefaultValue ? defaultValue : String.Empty);
                    if (saveDefaultValue)
                    {
                        Save();
                    }
                }
                //else if (_appConfig.AppSettings.Settings[key].Value.IsNullOrEmpty())
                //{
                //    _appConfig.AppSettings.Settings[key].Value = defaultValue == null ? String.Empty : defaultValue;
                //    Save();
                //}

                return(Configuration.AppSettings.Settings[key].Value.IsNullOrWhiteSpace() ? defaultValue : Configuration.AppSettings.Settings[key].Value);
            }
            catch (Exception ex)
            {
                ChoETLLog.Error(ex.ToString());
                return(defaultValue);
            }
        }
示例#3
0
 static ChoAppSettings()
 {
     try
     {
         if (ChoETLFrxBootstrap.Configuration == null)
         {
             if (HttpContext.Current == null)
             {
                 Configuration = ConfigurationManager.OpenExeConfiguration(null);
             }
             else
             {
                 Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
             }
         }
         else
         {
             Configuration = ChoETLFrxBootstrap.Configuration;
         }
     }
     catch (Exception ex)
     {
         ChoETLLog.Error(ex.ToString());
     }
 }
示例#4
0
        public static void SetValue(string key, string value)
        {
            ChoGuard.ArgumentNotNullOrEmpty(key, "Key");
            if (Configuration == null)
            {
                return;
            }

            try
            {
                if (value == null)
                {
                    value = String.Empty;
                }

                if (Configuration.AppSettings.Settings[key] == null)
                {
                    Configuration.AppSettings.Settings.Add(key, value);
                    Save();
                }
                else
                {
                    Configuration.AppSettings.Settings[key].Value = value;
                    Save();
                }
            }
            catch (Exception ex)
            {
                ChoETLLog.Error(ex.ToString());
            }
        }
示例#5
0
        private static void LoadReferencedAssemblies()
        {
            var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
            var loadedPaths      = loadedAssemblies.Select((a) =>
            {
                if (!a.IsDynamic)
                {
                    try
                    {
                        return(a.Location);
                    }
                    catch (Exception ex)
                    {
                        ChoETLLog.Error(ex.ToString());
                    }
                }
                return(String.Empty);
            }).ToArray();

            var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
            var toLoad          = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();

            toLoad.ForEach(path =>
            {
                try
                {
                    loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path)));
                }
                catch
                {
                }
            }
                           );
        }
示例#6
0
 private static void Save()
 {
     try
     {
         Configuation.Save(ConfigurationSaveMode.Modified);
         ConfigurationManager.RefreshSection("appSettings");
     }
     catch (Exception ex)
     {
         ChoETLLog.Error(ex.ToString());
     }
 }
示例#7
0
        private void SetDefaultValue(MemberInfo mi, bool saveDefaultValue = false)
        {
            object defaultValue = GetDefaultValue(mi.Name); // ChoType.GetRawDefaultValue(mi);

            try
            {
                ChoType.SetMemberValue(this, mi, defaultValue);
                ChoETLLog.Error("{0}: Assigned default value '{1}' to '{2}' property.".FormatString(NName, defaultValue.ToNString(), mi.Name));
            }
            catch (Exception ex)
            {
                ChoETLLog.Error("{0}: Error assigning default value '{1}' to '{2}' property. {3}".FormatString(NName, defaultValue.ToNString(), mi.Name, ex.Message));
            }
        }
示例#8
0
        public static bool Contains(string key)
        {
            ChoGuard.ArgumentNotNullOrEmpty(key, "Key");

            try
            {
                return(Configuration == null ? false : Configuration.AppSettings.Settings.AllKeys.Contains(key));
            }
            catch (Exception ex)
            {
                ChoETLLog.Error(ex.ToString());
                return(false);
            }
        }
        public Encoding GetEncoding(string fileName)
        {
            if (_encoding == null)
            {
                try
                {
                    ChoETLLog.Info("Determining '{0}' file encoding...".FormatString(fileName));
                    Encoding = ChoFile.GetEncodingFromFile(fileName);
                    ChoETLLog.Info("Found '{1}' encoding in '{0}' file.".FormatString(fileName, Encoding));
                }
                catch (Exception ex)
                {
                    Encoding = _defaultEncoding;
                    ChoETLLog.Error("Error finding encoding in '{0}' file. Default to UTF8.".FormatString(fileName));
                    ChoETLLog.Error(ex.Message);
                }
            }

            return(Encoding);
        }
示例#10
0
        private void Save()
        {
            lock (_padLock)
            {
                try
                {
                    if (ChoIniFile.New(this.IniFilePath).GetValue <bool>("IsLocked", false))
                    {
                        return;
                    }

                    Directory.CreateDirectory(Path.GetDirectoryName(_iniFilePath));

                    string iniContents = String.Empty;
                    if (File.Exists(_iniFilePath))
                    {
                        iniContents = File.ReadAllText(_iniFilePath);
                    }

                    if (_sectionName.IsNullOrWhiteSpace())
                    {
                        iniContents = Regex.Replace(iniContents, @"^(?<ini>[^\[]*)", Matcher, RegexOptions.IgnoreCase);
                        File.WriteAllText(_iniFilePath, iniContents);
                    }
                    else
                    {
                        if (!Regex.IsMatch(iniContents, @"(?<ini>\[{0}\][^\[]*)".FormatString(_sectionName)))
                        {
                            iniContents += "{0}[{1}]{0}".FormatString(Environment.NewLine, _sectionName);
                        }

                        iniContents = Regex.Replace(iniContents, @"(?<ini>\[{0}\][^\[]*)".FormatString(_sectionName), Matcher, RegexOptions.IgnoreCase);
                        File.WriteAllText(_iniFilePath, iniContents);
                    }
                }
                catch (Exception ex)
                {
                    ChoETLLog.Error(ex.ToString());
                }
            }
        }
示例#11
0
        public static Assembly[] LoadAssemblies(string[] directories)
        {
            List <Assembly> assemblies = new List <Assembly>();

            foreach (string directory in directories)
            {
                if (directory == null)
                {
                    continue;
                }
                foreach (string file in Directory.GetFiles(directory, "*.dll;*.exe", SearchOption.AllDirectories)) //TODO: Filter needs to be configurable
                {
                    if (file == null)
                    {
                        continue;
                    }

                    try
                    {
                        Assembly assembly = Assembly.LoadFile(file);
                        if (assembly != null)
                        {
                            DiscoverNLoadAssemblies(assembly, assemblies);
                            //fileProfile.Info(file);
                        }
                    }
                    catch (ChoFatalApplicationException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        ChoETLLog.Error(ex.ToString());
                    }
                }
            }
            return(assemblies.ToArray());
        }
示例#12
0
 private string GetSection()
 {
     lock (_padLock)
     {
         try
         {
             if (File.Exists(_iniFilePath))
             {
                 string iniContents = File.ReadAllText(_iniFilePath);
                 if (_sectionName.IsNullOrWhiteSpace())
                 {
                     Match m = Regex.Match(iniContents, @"^(?<ini>[^\[]*)");
                     if (m.Success)
                     {
                         return(m.Groups["ini"].Value);
                     }
                 }
                 else
                 {
                     var match = (from m in Regex.Matches(iniContents, @"^\[[^\]\r\n]+](?:\r?\n(?:[^[\r\n].*)?)*", RegexOptions.Multiline).AsTypedEnumerable <Match>()
                                  where m.Value.StartsWith("[{0}]".FormatString(_sectionName), StringComparison.CurrentCultureIgnoreCase)
                                  select m).FirstOrDefault();
                     return(match != null ? match.Value : String.Empty);
                     //Match m = Regex.Match(iniContents, @"(?<ini>\[{0}\][^\[]*)".FormatString(_sectionName), RegexOptions.IgnoreCase);
                     //if (m.Success)
                     //    return m.Groups["ini"].Value;
                 }
             }
         }
         catch (Exception ex)
         {
             ChoETLLog.Error(ex.ToString());
         }
     }
     return(String.Empty);
 }
示例#13
0
        private static Assembly LoadAssemblyFromResource(string name)
        {
            //Assembly thisAssembly = Assembly.GetEntryAssembly();

            foreach (Assembly thisAssembly in ChoAssembly.GetLoadedAssemblies())
            {
                if (thisAssembly.IsDynamic)
                {
                    continue;
                }
                try
                {
                    //Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder
                    var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name));
                    if (resources.Count() > 0)
                    {
                        var resourceName = resources.First();
                        using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName))
                        {
                            if (stream == null)
                            {
                                return(null);
                            }
                            var block = new byte[stream.Length];
                            stream.Read(block, 0, block.Length);
                            return(Assembly.Load(block));
                        }
                    }
                }
                catch (Exception ex)
                {
                    ChoETLLog.Error(ex.ToString());
                }
            }
            return(null);
        }
示例#14
0
        private void Initialize()
        {
            try
            {
                Dictionary <string, object> kvpDict = null;

                if (_watchChange)
                {
                    if (_func != null)
                    {
                        kvpDict = _func();
                    }
                    else
                    {
                        kvpDict = Seed();
                    }

                    if (kvpDict == null)
                    {
                        return;
                    }

                    Dictionary <string, object> mkvpDict = _kvpDict;
                    bool hasDiff = mkvpDict == null || kvpDict.Except(mkvpDict).Concat(mkvpDict.Except(kvpDict)).Any();
                    if (!hasDiff)
                    {
                        return;
                    }

                    _kvpDict = kvpDict;
                }
                else
                {
                    kvpDict = _kvpDict;
                }

                ERPSPropertyAttribute attr = null;
                object memberValue         = null;
                string propName            = null;
                //scan through members and load them
                foreach (var prop in ChoType.GetMembers(GetType()).Where(m => !m.HasAttribute(typeof(ChoIgnoreMemberAttribute)) && !ChoType.IsReadOnlyMember(m)))
                {
                    attr = ChoType.GetMemberAttribute <ERPSPropertyAttribute>(prop);
                    try
                    {
                        SetDefaultValue(prop, true);

                        propName = attr != null && !attr.Name.IsNullOrWhiteSpace() ? attr.Name : prop.Name;

                        if (kvpDict.ContainsKey(propName))
                        {
                            memberValue = AfterKVPLoaded(prop.Name, kvpDict[propName]);
                        }
                        else
                        {
                            memberValue = AfterKVPLoaded(prop.Name, null);
                        }

                        if (memberValue != null && memberValue is string)
                        {
                            string mv = memberValue as string;
                            if (attr != null)
                            {
                                switch (attr.TrimOption)
                                {
                                case ChoPropertyValueTrimOption.Trim:
                                    mv = mv.Trim();
                                    break;

                                case ChoPropertyValueTrimOption.TrimEnd:
                                    mv = mv.TrimEnd();
                                    break;

                                case ChoPropertyValueTrimOption.TrimStart:
                                    mv = mv.TrimStart();
                                    break;
                                }
                            }

                            memberValue = mv;
                        }

                        ChoType.ConvertNSetMemberValue(this, prop, memberValue);
                        ChoValidator.ValidateFor(this, prop);
                    }
                    catch (Exception ex)
                    {
                        ChoETLLog.Error("{0}: Error loading '{1}' property. {2}".FormatString(NName, prop.Name, ex.Message));
                        SetDefaultValue(prop, false);
                    }
                }
            }
            catch (Exception outerEx)
            {
                ChoETLLog.Error("{0}: Error loading options. {1}".FormatString(NName, outerEx.Message));
            }
        }