Home » 
        .Net » 
        C# Programs
    
    
    C# - How to Write Multiple (All) Lines to a File?
    
    
    
    
    
        Learn, how to write all lines into a text file using C# program?
        
            Submitted by IncludeHelp, on November 08, 2017 [Last updated : March 26, 2023]
        
    
    Given multiple lines and we have to write all lines into a text file.
    To write multiple (all) lines to a file in C#, we use File.WriteAllLines() method.
    File.WriteAllLines()
    This is a method of "File" class, which is used to write all lines in a text file.
Syntax
void WriteAllLines(string filename);
    Parameter(s)
    
        - filename - name of the file.
C# program to write multiple (all) lines to a file
using System;
using System.IO;
namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      string[] line = {
        "Sample Line 1",
        "Sample Line 2",
        "Sample Line 3",
        "Sample Line 4",
        "Sample Line 5"
      };
      File.WriteAllLines("sample.txt", line);
      Console.WriteLine("Data written successfully");
    }
  }
}
Output
Data written successfully
    
    Explanation
    In the above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.
	C# File Handling Programs »
	
	
    
    
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement