The File class static methods help you work with files easily without creating objects. You can create, read, write, copy, and delete files quickly.
File class static methods in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
File.MethodName(parameters);
File class methods are static, so you call them directly on the class without creating an object.
Common methods include ReadAllText, WriteAllText, Copy, Delete, and Exists.
Examples
C Sharp (C#)
string content = File.ReadAllText("example.txt");C Sharp (C#)
File.WriteAllText("output.txt", "Hello World!");
C Sharp (C#)
bool exists = File.Exists("data.csv");C Sharp (C#)
File.Copy("source.txt", "backup.txt", overwrite: true);
Sample Program
This program writes text to a file, checks if it exists, reads and prints the content, copies the file, and then deletes the original file.
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { string filePath = "testfile.txt"; // Write text to file File.WriteAllText(filePath, "Welcome to File class methods!"); // Check if file exists if (File.Exists(filePath)) { // Read text from file string content = File.ReadAllText(filePath); Console.WriteLine("File content:"); Console.WriteLine(content); // Copy file string copyPath = "copy_testfile.txt"; File.Copy(filePath, copyPath, overwrite: true); Console.WriteLine($"File copied to {copyPath}"); // Delete original file File.Delete(filePath); Console.WriteLine($"Original file {filePath} deleted."); } else { Console.WriteLine("File does not exist."); } } }
Important Notes
Always check if a file exists before reading or deleting to avoid errors.
When copying files, use the overwrite parameter carefully to avoid losing data.
File class methods work with paths, so ensure the path is correct and accessible.
Summary
File class static methods let you handle files easily without creating objects.
Common tasks include reading, writing, copying, checking existence, and deleting files.
Always handle files carefully to avoid errors or data loss.
Practice
1. Which of the following
File class static methods checks if a file exists at a given path?easy
Solution
Step 1: Understand the purpose of each method
File.Exists(path)checks if the file is present.File.ReadAllText(path)reads file content.File.Delete(path)deletes a file.File.Copy(source, destination)copies a file.Step 2: Identify the method that checks existence
The method that returns a boolean indicating if the file exists isFile.Exists(path).Final Answer:
File.Exists(path) -> Option AQuick Check:
Check file existence = File.Exists(path) [OK]
Hint: Exists method returns true if file is present [OK]
Common Mistakes:
- Confusing ReadAllText with Exists
- Using Delete to check existence
- Thinking Copy checks file presence
2. Which of the following is the correct syntax to write text to a file using the
File class?easy
Solution
Step 1: Recall the correct method name for writing text
TheFileclass usesWriteAllTextto write all text to a file at once.Step 2: Check method signatures
WriteText,Write, andWriteLineare not valid static methods ofFileclass.Final Answer:
File.WriteAllText(path, "Hello World") -> Option BQuick Check:
Write text to file = WriteAllText [OK]
Hint: Use WriteAllText to write full text at once [OK]
Common Mistakes:
- Using non-existent WriteText method
- Confusing File class with StreamWriter methods
- Using WriteLine which is not in File class
3. What will be the output of the following code if the file "test.txt" contains the text "Hello"?
string content = File.ReadAllText("test.txt");
Console.WriteLine(content);medium
Solution
Step 1: Understand what File.ReadAllText does
This method reads all text from the specified file and returns it as a string.Step 2: Analyze the code output
The variablecontentwill hold "Hello" from the file. The Console.WriteLine prints this string.Final Answer:
Hello -> Option AQuick Check:
ReadAllText returns file content [OK]
Hint: ReadAllText returns file content as string [OK]
Common Mistakes:
- Printing file name instead of content
- Expecting method name as output
- Assuming error without checking file existence
4. Identify the error in the following code snippet:
File.Copy("source.txt", "dest.txt");
File.Delete("source.txt");
File.Copy("source.txt", "dest.txt");medium
Solution
Step 1: Trace the file operations
First, source.txt is copied to dest.txt. Then source.txt is deleted. Finally, the code tries to copy source.txt again.Step 2: Identify the problem
After deletion, source.txt no longer exists, so the second copy call will throw aFileNotFoundException.Final Answer:
Second File.Copy will throw an exception because source.txt was deleted -> Option DQuick Check:
Copy after delete causes error [OK]
Hint: Cannot copy a file after deleting it [OK]
Common Mistakes:
- Assuming File.Copy works on deleted files
- Thinking order of Delete and Copy doesn't matter
- Believing .txt files cannot be copied
5. You want to create a backup of a file only if it exists, without overwriting an existing backup. Which code snippet correctly uses
File class static methods to do this?hard
Solution
Step 1: Understand the requirements
Backup only if original file exists and do not overwrite existing backup file.Step 2: Analyze each option
if (File.Exists("file.txt")) File.Copy("file.txt", "backup.txt", true); overwrites backup.txt because of 'true' overwrite flag. if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt"); checks existence of both files and copies only if backup.txt does not exist. File.Copy("file.txt", "backup.txt"); copies without checks, risking errors or overwrites. File.Copy("file.txt", "backup.txt", false); copies without overwrite but does not check if original file exists.Final Answer:
if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt"); -> Option CQuick Check:
Check both files before copy to avoid overwrite [OK]
Hint: Check both files exist before copying without overwrite [OK]
Common Mistakes:
- Not checking if backup file exists
- Using overwrite flag incorrectly
- Copying without checking original file existence
