File.CreateText Method in C#

Introduction

 
In this article, we will learn about File.CreateText method in C#.
 

File.CreateText Method in C#

 
The File.CreateText method creates and opens a file for writing UTF-8 encoded text. If the file already exists, this method opens the file.
 
The following code snippet creates a file using the CreateText method that returns a StreamWriter object. The WriteLine method of SteamLine can be used to add line text to the object and writes to the file.
 
Syntax 
  1. // Full file name  
  2. string fileName = @"C:\Temp\MaheshTX.txt";  
  3. try  
  4. {  
  5.     // Check if file already exists. If yes, delete it.  
  6.     if (File.Exists(fileName))  
  7.     {  
  8.         File.Delete(fileName);  
  9.     }  
  10.     // Create a new file  
  11.     using (StreamWriter sw = File.CreateText(fileName))  
  12.     {  
  13.         sw.WriteLine("New file created: {0}", DateTime.Now.ToString());  
  14.         sw.WriteLine("Author: Mahesh Chand");  
  15.         sw.WriteLine("Add one more line ");  
  16.         sw.WriteLine("Add one more line ");  
  17.         sw.WriteLine("Done! ");  
  18.     }  
  19.   // Write file contents on console.  
  20.     using (StreamReader sr = File.OpenText(fileName))  
  21.     {  
  22.         string s = "";  
  23.         while ((s = sr.ReadLine()) != null)  
  24.         {  
  25.             Console.WriteLine(s);  
  26.         }  
  27.     }  
  28. }  
  29. catch (Exception Ex)  
  30. {  
  31.     Console.WriteLine(Ex.ToString());  
  32. }  
This method is used to the StreamWriter(String, Boolean) constructor overload with the append parameter set to false. If the file specified by path does not exist, it is created. If the file does exist, its contents are overwritten. Additional threads are permitted to read the file while it is open.
 
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
 

Directory.GetCurrentDirectory Method

 
This Method is used the Gets the current working directory of the application.  
 
Syntax
  1. public static string GetCurrentDirectory ();    
Example
 
The following example demonstrates how to use the GetCurrentDirectory method.  
  1. using System;  
  2. using System.IO;  
  3.   
  4. class Test   
  5. {  
  6.     public static void Main()   
  7.     {  
  8.         try   
  9.         {  
  10.             // Get the current directory.  
  11.             string path = Directory.GetCurrentDirectory();  
  12.             string target = @"c:\temp";  
  13.             Console.WriteLine("The current directory is {0}", path);  
  14.             if (!Directory.Exists(target))   
  15.             {  
  16.                 Directory.CreateDirectory(target);  
  17.             }  
  18.   
  19.             // Change the current directory.  
  20.             Environment.CurrentDirectory = (target);  
  21.             if (path.Equals(Directory.GetCurrentDirectory()))   
  22.             {  
  23.                 Console.WriteLine("You are in the temp directory.");  
  24.             }   
  25.             else   
  26.             {  
  27.                 Console.WriteLine("You are not in the temp directory.");  
  28.             }  
  29.         }   
  30.         catch (Exception e)   
  31.         {  
  32.             Console.WriteLine("The process failed: {0}", e.ToString());  
  33.         }  
  34.     }  
  35. }   
The current directory is distinct from the original directory, which is the one from which the process was started.
 

Common I/O Tasks 

 
The System.IO namespace provides several classes that allow for various actions, such as reading and writing, to be performed on files, directories, and streams. For more information, see File and Stream I/O.
 

Summary 

 
In this article, we learned about File.CreateText method in C#.


Similar Articles