Ejemplo n.º 1
0
        /// <summary>
        /// Command line example: detect the encoding of the given file.
        /// </summary>
        /// <param name="filename">a filename</param>
        public static void DetectDemo(string filename)
        {
            // Detect from File
            DetectionResult result = CharsetDetector.DetectFromFile(filename);
            // Get the best Detection
            DetectionDetail resultDetected = result.Detected;

            // detected result may be null.
            if (resultDetected != null)
            {
                // Get the alias of the found encoding
                string encodingName = resultDetected.EncodingName;
                // Get the System.Text.Encoding of the found encoding (can be null if not available)
                Encoding encoding = resultDetected.Encoding;
                // Get the confidence of the found encoding (between 0 and 1)
                float confidence = resultDetected.Confidence;
                if (encoding != null)
                {
                    Console.WriteLine($"Detection completed: {filename}");
                    Console.WriteLine($"EncodingWebName: {encoding.WebName}{Environment.NewLine}Confidence: {confidence}");
                }
                else
                {
                    Console.WriteLine($"Detection completed: {filename}");
                    Console.WriteLine($"(Encoding is null){Environment.NewLine}EncodingName: {encodingName}{Environment.NewLine}Confidence: {confidence}");
                }
            }
            else
            {
                Console.WriteLine($"Detection failed: {filename}");
            }
        }
Ejemplo n.º 2
0
        public static Encoding GetEncodingType(string fileName)
        {
            DetectionResult result         = CharsetDetector.DetectFromFile(fileName);
            DetectionDetail resultDetected = result.Detected;

            if (resultDetected.Confidence < 0.7)
            {
                try
                {
                    return(Encoding.GetEncoding("GB18030"));
                }
                catch
                {
                    return(Encoding.GetEncoding("GB2312"));
                }
            }
            Encoding encoding = resultDetected.Encoding;

            return(encoding);
        }
Ejemplo n.º 3
0
        public void DetectionDetailGetEncodingIsNotNull(string codepageName)
        {
            var encoding = DetectionDetail.GetEncoding(codepageName);

            Assert.IsNotNull(encoding);
        }
Ejemplo n.º 4
0
 public JsonEncodingError(string path, DetectionDetail detail)
 {
     Path            = path;
     DetectionDetail = detail;
 }