Пример #1
0
        private void WriteToFile(Stream st)
        {
            try
            {
                XElement xRoot = new XElement(XN_ROOT);
                foreach (var pair1 in _storeValues)
                {
                    XElement   xSection = new XElement(XN_SECTION);
                    XAttribute xId      = new XAttribute(XN_ID, pair1.Key);
                    xSection.Add(xId);
                    xRoot.Add(xSection);

                    foreach (var pair2 in pair1.Value)
                    {
                        XElement   xKeyValue = new XElement(XN_KEYVALUE);
                        XAttribute xKey      = new XAttribute(XN_KEY, pair2.Key);
                        xKeyValue.Add(xKey);
                        XAttribute xValue = new XAttribute(XN_VALUE, pair2.Value);
                        xKeyValue.Add(xValue);
                        xSection.Add(xKeyValue);
                    }
                }

#if NET4
                xRoot.Save(st);
#else
                System.Xml.XmlWriter xrw = System.Xml.XmlWriter.Create(st, _xwSettings);
                xRoot.Save(st);
#endif
            }
            catch (Exception ex)
            {
                EventLogExceptionAdapter.WriteException(ex);
            }
        }
Пример #2
0
        public override void LoadInternal()
        {
            try
            {
                _isLoading = true;
                this.AcquireMutex();

                this.PerformWork(FileMode.Open, FileAccess.Read, ReadFromFile);
            }
            catch (Exception ex)
            {
                EventLogExceptionAdapter.WriteException(ex);
            }
            finally
            {
                this.ReleaseMutex();
                if (_isSavedByMe)
                {
                    _isSavedByMe = false;
                }
                if (_isLoading)
                {
                    _isLoading = false;
                }
                _fileModifiedTime = this.GetFileModifiedTime();
            }
        }
Пример #3
0
        public override void Save()
        {
            try
            {
                //Drive  AvailableFreeSpace <= 102400 [100Mb] Not update configration xml
                DriveInfo DrivesDetails = new DriveInfo(Path.GetPathRoot(System.Environment.GetEnvironmentVariable("WINDIR")).ToString());
                if (DrivesDetails.AvailableFreeSpace <= 102400)
                {
                    LogManager.WriteLog("Unable to update configration xml due to low disk space.", LogManager.enumLogLevel.Info);
                    return;
                }
                _isSavedByMe = true;
                this.AcquireMutex();

                this.PerformWork(FileMode.Create, FileAccess.Write, WriteToFile);
            }
            catch (Exception ex)
            {
                EventLogExceptionAdapter.WriteException(ex);
            }
            finally
            {
                this.ReleaseMutex();
            }
        }
Пример #4
0
        public static T GetAny <T>()
            where T : IConfigApplication
        {
            T    result  = default(T);
            Type typeOfT = typeof(T);

            try
            {
                result = Get <T>();
                if (result == null)
                {
                    foreach (var instance in _instances)
                    {
                        IConfigApplication value = instance.Value;
                        if (value != null)
                        {
                            var found = (from i in value.GetType().GetInterfaces()
                                         where i.FullName.IgnoreCaseCompare(typeOfT.FullName)
                                         select i).FirstOrDefault();
                            if (found != null)
                            {
                                result = (T)value;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                EventLogExceptionAdapter.WriteException(ex);
            }

            return(result);
        }
Пример #5
0
        private void PerformWork(FileMode mode, FileAccess access, Action <Stream> doWork)
        {
            Stream st = null;

            try
            {
                if (string.IsNullOrWhiteSpace(_directory))
                {
                    throw new Exception("BMCConfigPath not set");
                }

                if (!File.Exists(_fileName))
                {
                    access = FileAccess.Write;
                    st     = new FileStream(_fileName, FileMode.Create, access, FileShare.Read);
                }
                else
                {
                    int count = 0;
                    do
                    {
                        try
                        {
                            st = new FileStream(_fileName, mode, access, FileShare.Read);
                            break;
                        }
                        catch (System.IO.IOException)
                        {
                            count++;
                            System.Threading.Thread.Sleep(100);
                        }
                    } while (count < 10);
                }
                if (st != null)
                {
                    doWork(st);
                }
                else
                {
                    throw new Exception("Unable to open the file : " + _fileName);
                }
            }
            catch (Exception ex)
            {
                EventLogExceptionAdapter.WriteException(ex);
            }
            finally
            {
                if (st != null)
                {
                    st.Dispose();
                }
            }
        }
 private void LogException(ModuleProc PROC, Exception ex)
 {
     if (!_isLogging)
     {
         Log.Exception(PROC, ex);
     }
     else
     {
         EventLogExceptionAdapter.WriteException(ex);
     }
 }
 public static void Shutdown()
 {
     try
     {
         if (ShutdownInitiated != null)
         {
             ShutdownInitiated(null, EventArgs.Empty);
         }
     }
     catch (Exception ex)
     {
         EventLogExceptionAdapter.WriteException(ex);
     }
 }
Пример #8
0
        private DateTime GetFileModifiedTime()
        {
            DateTime fileModifiedTime = DateTime.MinValue;

            try
            {
                fileModifiedTime = File.GetLastWriteTime(_fileName);
            }
            catch (Exception ex)
            {
                EventLogExceptionAdapter.WriteException(ex);
            }
            return(fileModifiedTime);
        }
Пример #9
0
        private void ReadFromFile(Stream st)
        {
            try
            {
                if (st.Length > X_E_ROOT.Length)
                {
#if NET4
                    XElement xRoot = XElement.Load(st);
#else
                    System.Xml.XmlReader xrw   = System.Xml.XmlReader.Create(st, _xrSettings);
                    XElement             xRoot = XElement.Load(xrw);
#endif

                    if (xRoot != null && xRoot.Name == XN_ROOT)
                    {
                        var allValues = (from x in xRoot.Elements(XN_SECTION)
                                         from y in x.Elements(XN_KEYVALUE)
                                         select new XmlSectionKeyValue()
                        {
                            Section = x.Attribute(XN_ID).Value,
                            Key = y.Attribute(XN_KEY).Value,
                            Value = y.Attribute(XN_VALUE).Value
                        });
                        foreach (var allValue in allValues)
                        {
                            try
                            {
                                this.SetValue(allValue.Section, allValue.Key, allValue.Value, false);
                            }
                            catch (Exception ex)
                            {
                                EventLogExceptionAdapter.WriteException(ex);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                EventLogExceptionAdapter.WriteException(ex);
            }
        }
Пример #10
0
 void OnFileWathcer_FileModified(FileModificationWatcher watcher)
 {
     try
     {
         if (!_isSavedByMe)
         {
             this.Load();
         }
     }
     catch (Exception ex)
     {
         EventLogExceptionAdapter.WriteException(ex);
     }
     finally
     {
         if (_isSavedByMe)
         {
             _isSavedByMe = false;
         }
     }
 }
Пример #11
0
 protected override void ReloadIfModified()
 {
     if (_isLoading)
     {
         return;
     }
     try
     {
         // file modified time CR# 193726
         DateTime fileModifiedTimeNew = this.GetFileModifiedTime();
         bool     isModified          = (_fileModifiedTime.Ticks != fileModifiedTimeNew.Ticks);
         if (isModified)
         {
             LogManager.WriteLog(string.Format("{0} has been modified outside of this application at [{1}].", FILE_NAME, fileModifiedTimeNew.ToString("dd/MM/yyyy HH:mm:ss.fff")),
                                 LogManager.enumLogLevel.Warning);
             _fileModifiedTime = fileModifiedTimeNew;
             this.Load();
         }
     }
     catch (Exception ex)
     {
         EventLogExceptionAdapter.WriteException(ex);
     }
 }