Writing Files in C# Every Developer Should Know About | HOW TO – Code Samples



Writing Files in C# Every Developer Should Know About | HOW TO – Code Samples

Writing Files in C# Every Developer Should Know About | HOW TO - Code Samples

https://StartupHakk.com/?v=CgHEzOud53Y

#coding #codingbootcamp #softwaredeveloper #CodeYourFuture

GitHub Repo: https://github.com/slthomason/StartupHakk/tree/main/82_Write_Files_in_CSharp

To write files with C#, you have to know how to do it. The basics aren’t rocket science. There are a few lines of code you need to know. But writing and reading files with C# is something every beginning C# developer should be able to do.

It is pretty important you have some knowledge about C#. If you are a beginner, the chapters about encryption and decryption might be a bit too much.

I will be using C# and .NET 7 for this. My OS is Windows 11, which is a bit important since I don’t know how Linux works with special folders and the rights of creating and writing to files and folders.

I will start with a simple console application and work my way up from there. If you want to type along I suggest you create a console application first before you continue. I will be using .NET 7 and Visual Studio 2022 Community Preview.

To write files with C#, it really doesn’t matter what kind of file, you can use the StreamWriter. This class has several constructors with different parameters. The most used is the one that receives a path to a file. You can also set a Boolean if you want to append to the file (true) or empty the file before writing (false).

Default Encoding
If you want to set an encoding, the constructor is the place to set it. By default, the encoding is set to UTF8NoBOM. UTF-8 is a character encoding that represents each character in the Unicode standard using variable-length encoding The BOM stands for Byte Order Mark. The purpose of BOM is to indicate that the file has a UTF-8 encoding. The ‘No’ means it’s UTF-8 encoding without the byte order mark.

There is much more to UTF8NoBom, or its separate parts, but that’s not the goal of this video. If you change the encoding you need to keep this in mind. If you want to read from your file you need to set the encoding for specific encodings.

Filetypes
As I said before, it doesn’t really matter to what type of file you write. If you want to write to ‘my_awesome_file.klc’, go ahead. No problem there. But don’t use extensions that might exist. Don’t use .sln for example, or .doc. Use .txt or your own just to be safe.

Conclusion
Well, that was pretty cool, right? In this video, you have seen how you can easily write content from C# into a file. There are many reasons why you should write information to files. But always keep in mind the safety of your users: Never write sensitive information in a readable fashion to files.

Of course, there is much more to encryption, but this was an extra. But I do hope you got the idea. There are other ways of encrypting data, but AES is the most common and used in C# and .NET.

Lots of Great How Tos and Code Samples! Make sure to like and subscribe!