我有一个用C#编写的ASP.Net 4.0 Web应用程序,它所做的一件事就是根据从SQL Server 2012查询中提取的数据将文件写入文件系统。有时,用户输入包含来自法语的字符,经常从Microsoft Word剪切和粘贴,因此使用ANSI编码。然后,我的Web应用程序创建的此文件通过我无法控制的代码加载到另一个程序中,即不在我的Web应用程序中。问题是第二个程序需要UTF-8编码。我已编写代码将我的程序输出转换为UTF-8,但它仍未正确加载,所以我认为我做错了。这是我的代码:
protected void writeToClientFile(DataSet ClientGenl, DataSet ClientBus, DataSet ClientBill)
{
FileStream fileStream = null;
string fileName = "ClientTest.txt";
string pathName = ConfigurationSettings.AppSettings["EliteFilePath"].ToString();
try
{
using (new KLClassLibrary.Impersonator(proxyaccount, domain, password))
{
fileStream = OpenASAP(pathName + fileName, 10);
using (TextWriter tw = new StreamWriter(fileStream))
{
foreach (DataRow rowGeneral in ClientGenl.Tables[0].Rows)
{
string fileTextGeneral = "CLNUM:" + rowGeneral["clnum"].ToString().toEliteInput();
byte[] originalBytes = Encoding.Default.GetBytes(fileTextGeneral);
byte[] convertedBytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, originalBytes);
char[] convertedChars = new char[Encoding.UTF8.GetCharCount(convertedBytes, 0, convertedBytes.Length)];
Encoding.UTF8.GetChars(convertedBytes, 0, convertedBytes.Length, convertedChars, 0);
string convertedString = new string(convertedChars);
tw.WriteLine(convertedString);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fileStream != null)
fileStream.Dispose();
}
}
FileStream OpenASAP(string path, int maxTries)
{
FileStream fs = null;
bool lastResult = false;
int count = 0;
while ((lastResult == false) && count < maxTries)
{
lastResult = TryOpen(path, out fs);
Thread.Sleep(100);
count++;
}
if (!lastResult || count >= maxTries)
{
throw new Exception("The file is being written to");
}
return fs;
}
bool TryOpen(string FileWithPath, out FileStream fs)
{
try
{
fs = File.Open(FileWithPath, FileMode.Append, FileAccess.Write, FileShare.None);
return true;
}
catch (Exception ex)
{
fs = null;
return false;
}
}
答案 0 :(得分:1)
您可以从StreamReader.CurrentEncoding派生原始编码,然后使用Encoding.Convert()转换编码,将派生编码指定为源编码,并将Encoding.UTF8
指定为目标编码。 />
这样,您让StreamReader
决定哪种编码符合源文件的内容
它可能是Unicode(Windows中的UTF16),从默认编码转换而来。
string TextDestinaton = string.Empty;
using (FileStream FileOrigin = new FileStream(@"[SomeSourceFile]",
FileMode.Open, FileAccess.Read, FileShare.None))
using (StreamReader orgReader = new StreamReader(FileOrigin))
{
Encoding OriginalEncoding = Encoding.GetEncoding(orgReader.CurrentEncoding.CodePage);
byte[] OriginalBytes = OriginalEncoding.GetBytes(orgReader.ReadToEnd());
byte[] DestinationBytes = Encoding.Convert(OriginalEncoding, Encoding.UTF8, OriginalBytes, 0, OriginalBytes.Length);
using (MemoryStream memstream = new MemoryStream(DestinationBytes, 0, DestinationBytes.Length))
using (StreamReader destReader = new StreamReader(memstream, Encoding.UTF8))
{
memstream.Position = 0;
TextDestinaton = destReader.ReadToEnd();
};
}
作为注释:如果原始文本包含不适合本地编码的字符(获取文本时),并且没有使用特定的编码来保留源CodePage映射,则源文本可能会受到损害。 />