Ejemplo n.º 1
0
        public void Save()
        {
            if (ReadOnly)
            {
                return;
            }

            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            FileOperations.ClearFileAttributes(fileName);
            IntPtr file = IntPtr.Zero;

            try
            {
                try
                {
                    file = NativeMethods.FileCreateRewrite(fileName);
                    for (int i = 0; i < sections.Count; i++)
                    {
                        NativeMethods.FileWriteChar(file, openSection);
                        NativeMethods.FileWrite(file, sections.Key(i));
                        NativeMethods.FileWriteChar(file, closeSection);
                        NativeMethods.FileWriteNewLine(file);
                        for (int j = 0; j < sections[i].Count; j++)
                        {
                            if (!string.IsNullOrEmpty(sections[i].Value(j)))
                            {
                                NativeMethods.FileWrite(file, sections[i].Key(j));
                                NativeMethods.FileWriteChar(file, divider);
                                NativeMethods.FileWrite(file, sections[i].Value(j));
                                NativeMethods.FileWriteNewLine(file);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    TraceDebug.Trace("MemIniFile.Save: " + ex.Message);
                }
            }
            finally
            {
                if (file != IntPtr.Zero)
                {
                    NativeMethods.FileClose(file);
                }
            }
        }