2012-04-24 225 views
1

任何人都可以幫我嗎?我嘗試了很多不同的方式,但我沒有得到理想的結果。我只是想將現有文本[.txt]文件的編碼從ANSI更改爲UTF8,其中包含像ö,ü等字符。當我通過在編輯模式下打開該文本文件並手動執行FILE => SAVE AS時,它在編碼列表中顯示ANSI。使用這個,我可以將其編碼從ANSI更改爲UTF8,並且在這種情況下它不會更改任何內容/字符。但是當使用CODE時,它不起作用。將文本文件的編碼從ANSI更改爲UTF8,而不會影響C#中文件的任何字符!

==>第一種方法我用於實現由以下編碼:

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

但不幸的是,上述解決方案都沒有爲我工作。

回答

5

ANSI的問題在於它不是一種特定的編碼,它只是一個術語,表示「某些8位編碼是系統默認創建的」。

如果文件是在同一個系統上創建的,並且默認編碼沒有改變,那麼您可以使用Encoding.Default來讀取它,這樣你的第一個和第三個版本就可以工作。 (您的第二個版本只是複製文件而不做任何更改。)否則,您必須確切知道使用了哪種編碼。

這個例子使用了Windows的1250代碼頁:

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

可用編碼的列表,請參閱Encoding class的文檔。

-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); 
     } 
    } 
} 
+0

這不以任何方式適用於問如問。 – 2017-12-13 11:36:27

0

我有同樣的需求。這裏是我如何進行:

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); 
    } 
相關問題