誰かが私を助けることができますか?いろいろな方法を試しましたが、思い通りの結果が得られませんでした。既存の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();
しかし、残念ながら、上記の解決策はどれも私にはうまくいきませんでした。