1

誰かが私を助けることができますか?いろいろな方法を試しましたが、思い通りの結果が得られませんでした。既存のtext[.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);

==>私が使用した2番目の選択肢:

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);

            }
        }
    }
}

==>私が使用した3番目の選択肢:

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して読み取るだけで、最初と 3 番目のバージョンが機能します。(2 番目のバージョンでは、ファイルを変更せずにコピーするだけです。)それ以外の場合は、どのエンコーディングが使用されたかを正確に知る必要があります。

この例では、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 に答える