In this tutorial we will be working with files. How to create a file, edit it, rename a file make a copy an existing file or move it etc.
First thing you should do is to reference the System.IO namespace.
All the classes that will help you handle files are located in this namespace.
using System.IO;
How to create a file and edit it?
You can create a file like this:
string path = @"C:\test.txt"; File.Create(path);
This will create a file “test.txt” in C: drive. File.Create() method can be used to create all kind of files. It returns a FileStream instance which you can use to work with the new file. I mean you can also write the above code as FileStream stream = File.Create(path);
Remember, it is not a good practice to use File.Create() method without saving the FileStream reference in a variable. Because otherwise you won’t be able to close the stream and you will get this error if you work with the file:
The process cannot access the file because it is being used by another process.
Whenever you get that error check your code for streams that are not closed. Always close a stream using the Close() method when you are done with it. Or a recommended way is to use the using Statement (I’ve used it below).
Okay, we have created a file but it is empty which makes it useless. Let’s add something to it:
using (FileStream stream = File.Create(path)) { Byte[] text = new UTF8Encoding(true).GetBytes("Hello, World."); stream.Write(text, 0, text.Length); }
FileStream is not the best choice if you adding only text to a file. For more on the text files check the next part.
How to create a text file or open a text file and edit it?
Here we will use the File.CreateText() method to create the file. It returns a StreamWriter instance which allows you to write text to the file.
string path = @"C:\abc.txt"; // This makes a new file and then opens it for writing using (StreamWriter writer = File.CreateText(path)) { // Write a string to the file writer.Write("Welcome."); // Write a line writer.WriteLine("Hello, World"); writer.WriteLine("What's up?"); } // Open an existing file and write to it using (StreamWriter writer = File.Open(@"C:\xyz.txt")) { //... write text } // To append to an existing file using (StreamWriter writer = File.AppendText(@"C:\xyz.text")) { //... write text }
How to copy, move, delete and rename a file?
To move or copy a file you can use File.Copy() and File.Move() methods.
// Current path of the file string path = @"C:\abc.txt"; // New path for the file string newPath = @"F:\abc.txt"; // To avoid errors we shoud make sure the file exists. if (File.Exists(path)) { // Copy the file File.Copy(path, newPath); // Move the file File.Move(path, newPath); // Delete the file File.Delete(path); }
To rename a file we will use the File.Move() method:
// You use the same path but only rename the filename string currentName = @"C:\abc.txt"; string newName = @"C:\xyz.txt"; File.Move(currName, newName);
If you have any questions please don’t hesitate to post a comment.