Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] Learn how to read and write files in C#

Saturday 25 July 2015
|
Read: Completed in minutes

[Tips] Learn how to read and write files in C#

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.




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 ...

Learn how to read and write files in C#

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




Learn how to read and write files in C#

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


Learn how to read and write files in C#

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

Learn how to read and write files in C#


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 ....


Learn how to read and write files in C#



If We read this file with Default write Default mode. That's great. NO ERROR
So we can use it for file ASCII

Learn how to read and write files in C#

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


🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com