w3resource

C# Sharp File Handling : Exercises, Practice, Solution

C# Sharp File Handling [15 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

1. Write a program in C# Sharp to create a blank file on the disk.
Expected Output :

 A file created with name mytest.txt  

Click me to see the solution

2. Write a program in C# Sharp to remove a file from the disk.
Expected Output :

 Remove a file from the disk (at first create the file ) :                                                    
--------------------------------------------------------------                                                
 The file mytest.txt deleted successfully.
 
  Remove a file from the disk (at first create the file ) :                                                    
--------------------------------------------------------------                                                
 File does not exist 

Click me to see the solution

3. Write a C# Sharp program to create a blank file on the disk if the same file already exists.
Expected Output :

 A file created with name mytest.txt  

Click me to see the solution

4. Write a program in C# Sharp to create a file and add some text.
Expected Output:

 A file created with content name mytest.txt 

Click me to see the solution

5. Write a C# Sharp program to create a text file and read it.
Expected Output :

 Here is the content of the file mytest.txt :                                                                 
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt 

Click me to see the solution

6. Write a program in C# Sharp to create a file and write an array of strings to the file.
Test Data :
Input number of lines to write in the file :2
Input 2 strings below :
Input line 1 : this is 1st line
Input line 2 : this is 2nd line
Expected Output :

 The content of the file is  :                                                                                
----------------------------------                                                                            
 this is 1st line                                                                                             
 this is 2nd line 

Click me to see the solution

7. Write a C# Sharp program to create and write some lines of text into a file. This does not contain a given string on a line.
Test Data :
Input the string to ignore the line : fox
Input number of lines to write in the file : 2
Input 2 strings below :
Input line 1 : the quick brown fox jumps
Input line 2 : over the lazy dog.
Expected Output :

The line have ignored which contain the string 'fox'.                                                        
 The content of the file is  :                                                                                
----------------------------------                                                                            
 over the lazy dog.                                                      

Click me to see the solution

8. Write a program in C# Sharp to append some text to an existing file.
Expected Output :

 Here is the content of the file mytest.txt :                                                                 
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  

 Here is the content of the file after appending the text :                                                   
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  
 This is the line appended at last line. 

Click me to see the solution

9. Write a program in C# Sharp to create and copy the file to another name and display the content.
Expected Output :

 Here is the content of the file mytest.txt :                                                                 
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  

 The file mytest.txt successfully copied to the name mynewtest.txt in the same directory.                     
 Here is the content of the file mynewtest.txt :                                                              
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt

Click me to see the solution

10. Write a C# Sharp program to create a file and move it into the same directory with another name.
Expected Output:

Here is the content of the file mytest.txt :                                                                 
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  

 The file mytest.txt successfully moved to the name mynewtest.txt in the same directory.                      
 Here is the content of the file mynewtest.txt :                                                              
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt 

Click me to see the solution

11. Write a C# Sharp program to read the first line of a file.
Expected Output:

 Here is the content of the file mytest.txt :                                                                 
 test line 1                                                                                                  
 test line 2                                                                                                  
 Test line 3                                                                                                  

 The content of the first line of the file is :                                                               
 test line 1 

Click me to see the solution

12. Write a program in C# Sharp to create and read the last line of a file.
Test Data
Input number of lines to write in the file :3
Input 3 strings below :
Input line 1 : line1
Input line 2 : line2
Input line 3 : line3
Expected Output:

 The content of the last line of the file mytest.txt is  :                                                    
 line3  

Click me to see the solution

13. Write a program in C# Sharp to read a specific line from a file.
Test Data and Expected Output :

Input number of lines to write in the file  :3                                                               
 Input 3 strings below:                                                                                      
 Input line 1 : line1                                                                                         
 Input line 2 : line2                                                                                         
 Input line 3 : line3                                                                                         
  
 Input which line you want to display  :3                                                                     

 The content of the line 3 of the file mytest.txt is :                                                        
 line3          

Click me to see the solution

14. Write a C# Sharp program to create and read the last n lines of a file.
Test Data and Expected Output :

 Input number of lines to write in the file :5                                                               
 Input 5 strings below :                                                                                      
 Input line 1 : line1                                                                                         
 Input line 2 : line2                                                                                         
 Input line 3 : line3                                                                                         
 Input line 4 : line4                                                                                         
 Input line 5 : line5                                                                                         
 
 Input last how many numbers of lines you want to display  :3                                                  

 The content of the last 3 lines of the file mytest.txt is :                                                  
 The last no 3 line is : line3                                                                                
 The last no 2 line is : line4                                                                                
 The last no 1 line is : line5          

Click me to see the solution

15. Write a program in C# Sharp to count the number of lines in a file.
Expected Output:

 Here is the content of the file mytest.txt :                                                                 
 test line 1                                                                                                  
 test line 2                                                                                                  
 Test line 3                                                                                                  
 test line 4                                                                                                  
 test line 5                                                                                                  
 Test line 6                                                                                                  
 The number of lines in  the file mytest.txt is : 6         

Click me to see the solution

C# Sharp Code Editor

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.