Practice 50 C# File Handling Programming Questions, TechnoVlogs

Practice 50 C# File Handling Programming Questions


Q1. Write a C# program to create a text file and write "Hello, World!" into it.  
  Output: The file "output.txt" will contain the text "Hello, World!"

Q2. Write a C# program to read the content of a file and display it.  
  Input: File content: Hello, World!  
  Output: Hello, World!

Q3. Write a C# program to append "Welcome to C# Programming!" to an existing file.  
  Input: Existing file content: Hello, World!  
  Output: The file now contains Hello, World! Welcome to C# Programming!

Q4. Write a C# program to check if a file exists.  
  Input: File: example.txt  
  Output: File exists: True or File exists: False

Q5. Write a C# program to create a file and write multiple lines of text to it.  
  Output: The file "output.txt" will contain:  
  Line 1
  Line 2
  Line 3

Q6. Write a C# program to read and display the first line of a text file.  
  Input: File content:  
  Line 1
  Line 2
  Line 3

  Output: Line 1

Q7. Write a C# program to read and display all lines from a text file.  
  Input: File content:  
  Line 1
  Line 2
  Line 3

  Output:  
  Line 1
  Line 2
  Line 3

Q8. Write a C# program to delete a file.  
  Input: File: example.txt  
  Output: File deleted successfully

Q9. Write a C# program to create a file and write a string to it, then read and display the content.  
  Input: String: C# File Handling Example  
  Output: C# File Handling Example

Q10. Write a C# program to copy a file from one location to another.  
   Input: Source file: source.txt, Destination: destination.txt  
   Output: File source.txt copied to destination.txt

Q11. Write a C# program to move a file from one location to another.  
   Input: Source file: source.txt, Destination: destination.txt  
   Output: File source.txt moved to destination.txt

Q12. Write a C# program to get the size of a file in bytes.  
   Input: File: example.txt  
   Output: File Size: 1024 bytes

Q13. Write a C# program to rename a file.  
   Input: Old file name: oldname.txt, New file name: newname.txt  
   Output: File renamed to newname.txt

Q14. Write a C# program to check if a directory exists.  
   Input: Directory: C:\MyFolder  
   Output: Directory exists: True or Directory exists: False

Q15. Write a C# program to create a directory.  
   Input: Directory name: C:\NewDirectory  
   Output: Directory created successfully

Q16. Write a C# program to list all files in a directory.  
   Input: Directory: C:\MyFolder  
   Output: List of files in C:\MyFolder

Q17. Write a C# program to delete a directory.  
   Input: Directory: C:\MyFolder  
   Output: Directory deleted successfully

Q18. Write a C# program to check if a file is empty.  
   Input: File: empty.txt  
   Output: File is empty: True

Q19. Write a C# program to read the content of a file line by line using a loop.  
   Input: File content:  
   Line 1
   Line 2
   Line 3

   Output:      
   Line 1
   Line 2
   Line 3

Q20. Write a C# program to write numbers 1 to 5 to a file.  
   Output: The file numbers.txt will contain:  
   1
   2
   3
   4
   5

Q21. Write a C# program to read a file and count the number of words in it.  
   Input: File content: Hello World! C# is amazing.  
   Output: Word Count: 5

Q22. Write a C# program to find and replace text in a file.  
   Input: File content: Hello World!, Replace World with C#  
   Output: Hello C#!

Q23. Write a C# program to create a file and write user input into it.  
   Input: User input: My first file  
   Output: The file will contain My first file

Q24. Write a C# program to create a file and append user input to it.  
   Input: User input: Additional text  
   Output: The file will contain Additional text appended to the existing content.

Q25. Write a C# program to display the creation time of a file.  
   Input: File: example.txt  
   Output: File Creation Time: 03/01/2025 10:45:30

Q26. Write a C# program to display the last access time of a file.  
   Input: File: example.txt  
   Output: Last Access Time: 03/01/2025 10:45:30

Q27. Write a C# program to display the last write time of a file.  
   Input: File: example.txt  
   Output: Last Write Time: 03/01/2025 10:45:30

Q28. Write a C# program to read a file in binary format and display its contents.  
   Input: File: binaryfile.bin  
   Output: Binary content displayed as a sequence of bytes.

Q29. Write a C# program to copy a file only if the file exists.  
   Input: Source file: source.txt, Destination: destination.txt  
   Output: File copied successfully or File does not exist.

Q30. Write a C# program to open a file in read-only mode.  
   Output: File opened in read-only mode.

Q31. Write a C# program to create a file and write a list of numbers to it using a loop.  
   Output: The file will contain numbers from 1 to 10.

Q32. Write a C# program to check if a file is a directory.  
   Input: File: C:\MyFolder\example.txt  
   Output: Is Directory: False

Q33. Write a C# program to read the content of a file and display the number of characters.  
   Input: File content: Hello World!  
   Output: Character Count: 12

Q34. Write a C# program to read a file and display the number of lines.  
   Input: File content:  
   Line 1
   Line 2
   Line 3

   Output: Line Count: 3

Q35. Write a C# program to write a list of strings to a file.  
   Input: List: ["Hello", "C#", "File Handling"]  
   Output: The file will contain:  
   Hello
   C#
   File Handling

Q36. Write a C# program to read a file and display only even-numbered lines.  
   Input: File content:  
   Line 1
   Line 2
   Line 3
   Line 4

   Output:  
   Line 2
   Line 4

Q37. Write a C# program to create a file and write user input until "exit" is entered.  
   Input: User input: My first entry (Repeat until "exit")  
   Output: The file will contain:      
   My first entry

Q38. Write a C# program to open a file, read it, and then close it.  
   Output: The file is opened, read, and closed successfully.

Q39. Write a C# program to write a string to a file in UTF8 encoding.  
   Input: String: UTF8 Example  
   Output: The file output.txt will contain UTF8 Example in UTF8 encoding.

Q40. Write a C# program to list all directories in a given directory.  
   Input: Directory: C:\MyFolder  
   Output: List of directories in C:\MyFolder

Q41. Write a C# program to create a file with a custom extension.  
   Input: File name: mydata.custom  
   Output: The file mydata.custom is created.

Q42. Write a C# program to change the file attributes (readonly, hidden) of a file.  
   Input: File: example.txt  
   Output: File attributes changed to readonly and hidden.

Q43. Write a C# program to read a file and find the longest line.  
   Input: File content:  
   Line 1
   Longest line in this file
   Line 3

   Output: Longest Line: Longest line in this file

Q44. Write a C# program to read a file and remove all blank lines.  
   Input: File content:  
   Line 1
   Line 2

   Output: 
   Line 1
   Line 2

Q45. Write a C# program to read a file and display its content in reverse order.  
   Input: File content:  
   Line 1
   Line 2
   Line 3

   Output: 
   Line 3
   Line 2
   Line 1

Q46. Write a C# program to read a binary file and display its size.  
   Input: Binary file: image.jpg  
   Output: File Size: 1024 bytes

Q47. Write a C# program to find the file extension of a file.  
   Input: File: document.pdf  
   Output: File Extension: .pdf

Q48. Write a C# program to find the full path of a file.  
   Input: File: example.txt  
   Output: Full Path: C:\MyFolder\example.txt

Q49. Write a C# program to open a file and count the number of occurrences of a word.  
   Input: File content: C# is great. C# is easy.  
   Output: Occurrences of 'C#': 2

Q50. Write a C# program to create a backup of an existing file.  
   Input: File: document.txt  
   Output: A backup file document_backup.txt is created.

Share on Social Media