I have an ASP.Net 4.0 web application, written in C#, and one of the things it does is write a file to the file system based on data pulled from a SQL Server 2012 query. Sometimes the user input contains characters from French, frequently cut and pasted from Microsoft Word and therefore in ANSI encoding. This file my web application creates is then loaded into another program via code outside of my control, i.e., not in my web application. The problem is that this second program requires UTF-8 encoding. I've written code to convert my program's output to UTF-8, but it's still not loading correctly, so I think I'm doing something wrong. Here's my code:
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;
}
}