File Operations In C#

This article will explain simple file operations like creating folder, creating file, appending file and deleting file, using C# code.

Here, we will also check if the Folder and File already exist or not. I hope this code will help a bit in your daily programming life as it is most commonly used.

Create console application and replace the code in Program.cs file.

Program.cs file code

  1. using System;  
  2. using System.Text;  
  3. using System.IO;  
  4.   
  5. namespace FileOperations  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             string folderPath = @"c:\\RKFolder";  
  12.             string fileName = "OutputFile.txt";  
  13.             string filePath = System.IO.Path.Combine(folderPath, fileName);  
  14.   
  15.             try  
  16.             {  
  17.                 CreateFolder(folderPath);  
  18.                 CreateFile(filePath, fileName);  
  19.   
  20.                 // Adding data to the file  
  21.                 string message = string.Empty;  
  22.                 for (byte i = 0; i < 10; i++)  
  23.                 {  
  24.                     message = "Sample Text - " + i;  
  25.                     AppendTextToFile(filePath, message);  
  26.                 }  
  27.                 Console.WriteLine("\nFile successfully updated on path:\t " + filePath);  
  28.                 Console.WriteLine("Press Enter to Exit...");  
  29.                 Console.ReadLine();  
  30.             }  
  31.             catch (Exception ex)  
  32.             {  
  33.                 Console.WriteLine("Ufff, something went wrong...");  
  34.                 Console.WriteLine(ex.Message);  
  35.                   
  36.                 Console.WriteLine("Press Enter to Exit...");  
  37.                 Console.ReadLine();  
  38.             }  
  39.         }  
  40.         static void AppendTextToFile(string filePath, string message)  
  41.         {  
  42.             // Open and append the file  
  43.             using (System.IO.FileStream fs = new System.IO.FileStream(filePath, FileMode.Append))  
  44.             {  
  45.                 byte[] info = new UTF8Encoding(true).GetBytes(System.Environment.NewLine + message);  
  46.                 fs.Write(info, 0, info.Length);  
  47.             }  
  48.         }  
  49.         private static void CreateFolder(string folderPath)  
  50.         {  
  51.             // Create folder if not present  
  52.             bool folderExists = Directory.Exists(folderPath);  
  53.             if (!folderExists)  
  54.                 Directory.CreateDirectory(folderPath);  
  55.         }  
  56.   
  57.         private static void CreateFile(string filePath, string fileName)  
  58.         {  
  59.             if (!System.IO.File.Exists(filePath))  
  60.             {  
  61.                 // Create file  
  62.                 System.IO.FileStream fs = System.IO.File.Create(filePath);  
  63.                 fs.Close();  
  64.             }  
  65.             else  
  66.             {  
  67.                 // Delete the file  
  68.                 Console.WriteLine("File \"{0}\" already exists.", fileName);  
  69.                 File.Delete(filePath);  
  70.                 Console.WriteLine("File deleted...");  
  71.   
  72.                 // Create the file  
  73.                 System.IO.FileStream fs = System.IO.File.Create(filePath);  
  74.                 fs.Close();  
  75.                 Console.WriteLine("File re-created...");  
  76.                 return;  
  77.             }  
  78.         }  
  79.     }  
  80. }  

Execute the project. Given below is the output.

File Operations In C#
 
File Operations In C#