Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Create a file and write an array of strings

C# Sharp File Handling: Exercise-6 with Solution

Write a program in C# Sharp to create a file and write an array of strings to the file.

Sample Solution:-

C# Sharp Code:

using System;
using System.IO;


class WriteTextFile
{
    static void Main()
    {
		string fileName = @"mytest.txt"; 
		string[] ArrLines ;
		int n,i;

	    Console.Write("\n\n Create a file and write an array of strings  :\n");
		Console.Write("---------------------------------------------------\n"); 

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

       Console.Write(" Input number of lines to write in the file  :");
       n= Convert.ToInt32(Console.ReadLine()); 
       ArrLines=new string[n];      
       Console.Write(" Input {0} strings below :\n",n);
		for(i=0;i<n;i++)
		{
		Console.Write(" Input line {0} : ",i+1);
		ArrLines[i] = Console.ReadLine();	
		}	
        System.IO.File.WriteAllLines(fileName, ArrLines);
        
	using (StreamReader sr = File.OpenText(fileName)) 
        {
            string s = "";
             Console.Write("\n The content of the file is  :\n",n);
             Console.Write("----------------------------------\n");
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(" {0} ",s);
            }
            Console.WriteLine();
        }        
    }
}

Sample Output:

 Create a file and write an array of strings  :                                                               
---------------------------------------------------                                                           
 Input number of lines to write in the file  :2                                                               
 Input 2 strings below :                                                                                      
 Input line 1 : This is a 1st line                                                                            
 Input line 2 : This is a 2nd line                                                                            
   
 The content of the file is  :                                                                                
----------------------------------                                                                            
 This is a 1st line                                                                                           
 This is a 2nd line 

Flowchart :

Flowchart: C# Sharp Exercises -  Create a file and write an array of strings.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to create a file with text and read the file.
Next: Write a program in C# Sharp to create and write some line of text into a file which does not contain a given string in a line.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.