Read, Write file in C# is common tech skill in every developer, in some country we have some mode of languages (TCVN3, VNI windows, Unicode, ...). A file containing the language and character encoding really complicated if you do not really grasp it. You're vulnerable to false when read and store files with native languages.
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:
This topic focuses on how to read and write files with TCVN3, Unicode. As well as compare the read and write mode in C#:
The mode will be compare: Unicode, UTF7, UTF8, UTF32, BigEndianUnicode, Default, ASCII in C#
I have two function read, write files text with encoding Unicode, UTF7, UTF8, UTF32, BigEndianUnicode, Default, ASCII in C#
Source code
/* *************************************************************
* Author: Zidane (huuvi168@gmail.com)
* Last Modified: 20150525
* *************************************************************/
/// <summary>
/// Write szContent in file with szPath (encoding)
/// </summary>
/// <param name="szPath" /> Path of files
/// <param name="szContent" /> content of files
/// <param name="Mode" /> 1: CreateNew / 2:Append
/// <returns>bool: true -> succeed </returns>
static public bool WriteFile(string szPath, string szContent,
int iMode, Encoding en)
{
bool bFlag = false;
FileStream fs = null;
try
{
switch (iMode)
{
case 1:
fs = new FileStream(szPath, FileMode.Create);
break;
case 2:
fs = new FileStream(szPath, FileMode.Append);
break;
default:
fs = new FileStream(szPath, FileMode.Append);
break;
}
using (StreamWriter sw = new StreamWriter(fs, en))
{
sw.WriteLine(szContent);
sw.Close();
}
fs.Close();
bFlag = true;
}
catch
{
return bFlag;
}
return bFlag;
}
/// <summary>
/// Read file text with szPath
/// </summary>
/// <param name="szLog" /> path of log files
/// <param name="szPath" /> path of files must read
/// <param name="en" /> encoding of files
/// <returns> string: content in files</returns>
public static string ReadTextFile(string szLog, string szPath, Encoding en)
{
string szContent = "";
try
{
FileStream fs = new FileStream(szPath, FileMode.Open);
if (fs == null)
return null;
using (StreamReader sr = new StreamReader(fs, en))
{
szContent = sr.ReadToEnd();
sr.BaseStream.Close();
}
}
catch (Exception ex)
{
CommonLib.clsMain.WriteLog(szLog, ex);
}
return szContent;
}
Compare
Read file save as Unicode mode:
We have file write in Unicode mode! So we use function above for read and write file. We have some below result:If We read this file with UTF32, UTF8, UTF7, Unicode, BigEndianUnicode, Default, ASCII. When write with Default mode you can see result...Some characters will be lost ...
If We read this file with UTF32, UTF8, UTF7, Unicode, BigEndianUnicode, Default, ASCII. When write with Default mode. That's great! NO ERROR
So we can use it for file Unicode
Read file save as ASCII mode: (TCVN3)
We have file write in ASCII mode! So we use function above for read and write file. We have some below result:If We read this file with UTF7 write Default or ASCII mode. You can see result...Some characters will be lost on the red frame
We have file write in UTF8 / ASCII mode Write Default or
file write in UTF8 / ASCII / Default mode Write ASCII
So we use function above for read and write file. We have some below result:Lost many character, NOT GOOD
We have file write in UTF32 / Unicode / BigEndianUnicode mode Write Default or
file write in UTF32 / BigEndianUnicode / Unicode mode Write ASCIIWe have file write in ASCII mode! So we use function above for read and write file. We have below result, all see questions signal ....
If We read this file with Default write Default mode. That's great. NO ERROR
So we can use it for file ASCII
FINALLY CONCLUDE:
- File save as with Unicode -> Using any mode ...- File save as with ASCII -> Using Read Default, Write Default
See more: https://learn-tech-tips.blogspot.com/2015/05/readwritefileinCsharp.html
Are you interested in topic Learn how to read and write files in C# from Webzone Tech Tips? If you have any thoughts or questions, please share them in the comment section below. I would love to hear from you and chat about it
Webzone Tech Tips Zidane, all things tech tips web development