Ejemplo n.º 1
0
        private void LoadEncoding()
        {
            var includedEncoding = AppSettings.AvailableEncodings;

            ListIncludedEncodings.BeginUpdate();
            try
            {
                ListIncludedEncodings.Items.AddRange(includedEncoding.Values.ToArray <object>());
                ListIncludedEncodings.DisplayMember = nameof(Encoding.EncodingName);
            }
            finally
            {
                ListIncludedEncodings.EndUpdate();
            }

            var availableEncoding = Encoding.GetEncodings()
                                    .Select(ei => ei.GetEncoding())
#pragma warning disable SYSLIB0001                                  // Type or member is obsolete
                                    .Where(e => e != Encoding.UTF7) // UTF-7 is no longer supported, see: https://github.com/dotnet/docs/issues/19274
#pragma warning restore SYSLIB0001                                  // Type or member is obsolete
                                    .Where(e => !includedEncoding.ContainsKey(e.HeaderName))
                                    .ToList();

            // If exists utf-8, then replace to utf-8 without BOM
            var utf8 = availableEncoding.FirstOrDefault(e => typeof(UTF8Encoding) == e.GetType());

            if (utf8 is not null)
            {
                availableEncoding.Remove(utf8);
                availableEncoding.Add(new UTF8Encoding(false));
            }

            ListAvailableEncodings.BeginUpdate();
            try
            {
                ListAvailableEncodings.Items.AddRange(availableEncoding.ToArray <object>());
                ListAvailableEncodings.DisplayMember = nameof(Encoding.EncodingName);
            }
            finally
            {
                ListAvailableEncodings.EndUpdate();
            }
        }
Ejemplo n.º 2
0
        private void LoadEncoding()
        {
            var includedEncoding = AppSettings.AvailableEncodings;

            ListIncludedEncodings.BeginUpdate();
            try
            {
                ListIncludedEncodings.Items.AddRange(includedEncoding.Values.ToArray());
                ListIncludedEncodings.DisplayMember = nameof(Encoding.EncodingName);
            }
            finally
            {
                ListIncludedEncodings.EndUpdate();
            }

            var availableEncoding = Encoding.GetEncodings()
                                    .Select(ei => ei.GetEncoding())
                                    .Where(e => !includedEncoding.ContainsKey(e.HeaderName))
                                    .ToList();

            // If exists utf-8, then replace to utf-8 without BOM
            var utf8 = availableEncoding.FirstOrDefault(e => typeof(UTF8Encoding) == e.GetType());

            if (utf8 != null)
            {
                availableEncoding.Remove(utf8);
                availableEncoding.Add(new UTF8Encoding(false));
            }

            ListAvailableEncodings.BeginUpdate();
            try
            {
                ListAvailableEncodings.Items.AddRange(availableEncoding.ToArray());
                ListAvailableEncodings.DisplayMember = nameof(Encoding.EncodingName);
            }
            finally
            {
                ListAvailableEncodings.EndUpdate();
            }
        }
        private void LoadEncoding()
        {
            var includedEncoding = AppSettings.AvailableEncodings;

            ListIncludedEncodings.BeginUpdate();
            try
            {
                ListIncludedEncodings.Items.AddRange(includedEncoding.Values.ToArray <object>());
                ListIncludedEncodings.DisplayMember = nameof(Encoding.EncodingName);
            }
            finally
            {
                ListIncludedEncodings.EndUpdate();
            }

            object[] selectableEncodings = Encoding.GetEncodings()
                                           .Select(ei => ei.GetEncoding())
                                           .Select(e => e.GetType() == typeof(UTF8Encoding) ? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) : e) // If exists utf-8, then replace to utf-8 without BOM
#pragma warning disable SYSLIB0001                                                                                                                          // Type or member is obsolete
                                           .Where(e => e != Encoding.UTF7)                                                                                  // UTF-7 is no longer supported, see: https://github.com/dotnet/docs/issues/19274
#pragma warning restore SYSLIB0001                                                                                                                          // Type or member is obsolete
                                           .Where(e => !includedEncoding.ContainsKey(e.WebName))
                                           .GroupBy(e => e.WebName)
                                           .Select(group => group.First()) // ignore encodings which cannot be distinguished, keep first only
                                           .ToArray <object>();

            ListAvailableEncodings.BeginUpdate();
            try
            {
                ListAvailableEncodings.Items.AddRange(selectableEncodings);
                ListAvailableEncodings.DisplayMember = nameof(Encoding.EncodingName);
            }
            finally
            {
                ListAvailableEncodings.EndUpdate();
            }
        }