Пример #1
0
 /// <summary>
 ///     Writes the string representation of a specific <see cref="IniDocument"/>
 ///     object to the specified destination file.
 /// </summary>
 /// <param name="source">
 ///     The <see cref="IniDocument"/> object to write.
 /// </param>
 /// <param name="destFile">
 ///     The destination file to be written.
 /// </param>
 public static bool WriteTo(this IniDocument source, string destFile)
 {
     try
     {
         if (source == null)
         {
             throw new ArgumentNullException(nameof(source));
         }
         if (source.Count == 0)
         {
             throw new ArgumentInvalidException(nameof(source));
         }
         var path = PathEx.Combine(destFile);
         if (string.IsNullOrEmpty(path))
         {
             throw new ArgumentNullException(nameof(destFile));
         }
         if (!PathEx.IsValidPath(path))
         {
             throw new ArgumentInvalidException(nameof(destFile));
         }
         using var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
         WriteTo(source, fs);
         return(true);
     }
     catch (Exception ex) when(ex.IsCaught())
     {
         Log.Write(ex);
     }
     return(false);
 }
Пример #2
0
 /// <summary>
 ///     Converts the content of an INI file or an INI file formatted string value
 ///     to an equivalent <see cref="IniDocument"/> object.
 /// </summary>
 /// <param name="fileOrContent">
 ///     The path or content of an INI file.
 /// </param>
 /// <param name="result">
 ///     The result <see cref="IniDocument"/> object.
 /// </param>
 public static bool TryParse(string fileOrContent, out IniDocument result)
 {
     try
     {
         result = Parse(fileOrContent);
         return(result != null);
     }
     catch (Exception ex) when(ex.IsCaught())
     {
         result = default;
         Log.Write(ex);
     }
     return(false);
 }
Пример #3
0
        /// <summary>
        ///     Writes the string representation of a specific <see cref="IniDocument"/>
        ///     object to the specified destination stream.
        /// </summary>
        /// <param name="source">
        ///     The <see cref="IniDocument"/> object to write.
        /// </param>
        /// <param name="destStream">
        ///     The destination stream to be written.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     source or destStream is null.
        /// </exception>
        public static void WriteTo(this IniDocument source, Stream destStream)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destStream == null)
            {
                throw new ArgumentNullException(nameof(destStream));
            }
            var fileFormat = source.FileFormat;

            using var sw = new StreamWriter(destStream, fileFormat == IniFileFormat.Regedit4 ? EncodingEx.Ansi : EncodingEx.Utf8NoBom, 8192, true);
            switch (fileFormat)
            {
            case IniFileFormat.Regedit4:
                sw.WriteLine("REGEDIT4");
                sw.WriteLine();
                break;

            case IniFileFormat.Regedit5:
                sw.WriteLine("Windows Registry Editor Version 5.00");
                sw.WriteLine();
                break;
            }
            foreach (var section in source.Sections)
            {
                if (IniHelper.SectionIsInvalid(section))
                {
                    continue;
                }
                var keyValueDict = source[section];
                var hasNoValue   = !keyValueDict?.Any() ?? true;
                if (hasNoValue && !IniHelper.IsImportantSection(section))
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(section))
                {
                    sw.Write('[');
                    sw.Write(section.Trim());
                    sw.WriteLine(']');
                }
                if (hasNoValue)
                {
                    sw.WriteLine();
                    continue;
                }
                foreach (var pair in keyValueDict)
                {
                    if (IniHelper.KeyIsInvalid(pair.Key) || !pair.Value.Any())
                    {
                        continue;
                    }
                    foreach (var value in pair.Value)
                    {
                        if (string.IsNullOrEmpty(value) || value.Any(TextEx.IsLineSeparator))
                        {
                            continue;
                        }
                        sw.Write(pair.Key.Trim());
                        sw.Write('=');
                        if (!IniHelper.IsHex(value) || value.Length < 76)
                        {
                            sw.WriteLine(value);
                        }
                        else
                        {
                            for (var i = 0; i < value.Length; i++)
                            {
                                sw.Write(value[i]);
                                if (i == 0 || i % 75 != 0)
                                {
                                    continue;
                                }
                                sw.WriteLine('\\');
                            }
                            sw.WriteLine();
                        }
                    }
                }
                sw.WriteLine();
            }
        }