1

谁能帮我吗?我尝试了很多不同的方法,但我没有得到想要的结果。我只想将现有文本 [.txt] 文件的编码从 ANSI 更改为 UTF8,其中包含 ö、ü 等字符。当我通过在编辑模式下打开该文本文件然后 FILE=>SAVE AS 手动执行此操作时,它在编码列表中显示 ANSI。使用它,我可以将其编码从 ANSI 更改为 UTF8,在这种情况下它不会更改任何内容/字符。但是在使用 CODE 时,它不起作用。

==>我曾经通过以下代码实现这一目标的第一种方式:

if (!System.IO.Directory.Exists(System.Windows.Forms.Application.StartupPath + "\\Temp"))
{
    System.IO.Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + "\\Temp");
}
string destPath = System.Windows.Forms.Application.StartupPath + "\\Temp\\temporarytextfile.txt";

File.WriteAllText(destPath, File.ReadAllText(path, Encoding.Default), Encoding.UTF8);

==> 我使用的第二种选择:

using (Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (Stream destStream = new FileStream(destPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
    {
        using (var reader = new BinaryReader(fileStream, Encoding.Default))
        {
            using (var writer = new BinaryWriter(destStream, Encoding.UTF8))
            {
                var srcBytes = new byte[fileStream.Length];
                reader.Read(srcBytes, 0, srcBytes.Length);
                writer.Write(srcBytes);

            }
        }
    }
}

==> 我使用的第三种选择:

System.IO.StreamWriter file = new System.IO.StreamWriter(destPath, true, Encoding.Default);
using (StreamReader sr = new StreamReader(path, Encoding.UTF8, true))
{
    String line1;
    while ((line1 = sr.ReadLine()) != null)
    {
        file.WriteLine(line1);
    }
}

file.Close();

但不幸的是,上述解决方案都不适合我。

4

3 回答 3

7

ANSI 的问题在于它不是一种特定的编码,它只是“某些 8 位编码是创建它的系统的默认值”的术语。

如果文件是在同一个系统上创建的,并且默认编码没有改变,你可以用Encoding.Default它来读取它,所以你的第一个和第三个版本都可以工作。(您的第二个版本只是复制文件而不做任何更改。)否则您必须确切知道使用了哪种编码。

此示例使用 windows-1250 代码页:

File.ReadAllText(path, Encoding.GetEncoding(1250))

有关可用编码的列表,请参阅Encoding 类的文档。

于 2012-04-24T11:09:16.880 回答
1

我也有同样的需要。这是我如何进行的:

    int Encode(string file, Encoding encode)
    {
        int retour = 0;
        try
        {
            using (var reader = new StreamReader(file))
            {
                if (reader.CurrentEncoding != encode)
                {
                    String buffer = reader.ReadToEnd();
                    reader.Close();
                    using (StreamWriter writer = new System.IO.StreamWriter(file, false, encode))
                    {
                        writer.Write(buffer);
                        writer.Close();
                    }
                    message = string.Format("Encode {0} !", file);
                    retour = 2;
                }
                else retour = 1;
            }
        }
        catch(Exception e)
        {
            message = string.Format("{0} ?", e.Message);
        }
        return retour;
    }

    /// <summary>
    /// Change encoding to UTF8
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    public int toUTF8(string file)
    {
        return Encode(file, Encoding.UTF8);
    }

    public int toANSI(string file)
    {
        return Encode(file, Encoding.Default);
    }
于 2017-12-13T10:15:40.800 回答
-1

您是否尝试过以下方法:

http://msdn.microsoft.com/en-us/library/system.text.encoding.convert%28v=vs.71%29.aspx

using System;
using System.Text;
namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";

         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         Encoding unicode = Encoding.Unicode;

         // Convert the string into a byte[].
         byte[] unicodeBytes = unicode.GetBytes(unicodeString);

         // Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);

         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}
于 2012-04-24T11:11:17.300 回答