Bird
Raised Fist0
C Sharp (C#)programming~10 mins

File class static methods in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a file exists at the given path.

C Sharp (C#)
bool exists = File.[1]("example.txt");
Drag options to blanks, or click blank then click option'
AReadAllText
BExists
CDelete
DCopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReadAllText instead of Exists causes runtime error if file missing.
Using Delete or Copy does not check existence.
2fill in blank
medium

Complete the code to read all text from a file into a string.

C Sharp (C#)
string content = File.[1]("data.txt");
Drag options to blanks, or click blank then click option'
AReadAllText
BWriteAllText
CAppendAllText
DDelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using WriteAllText tries to write, not read.
AppendAllText adds text instead of reading.
3fill in blank
hard

Fix the error in the code to copy a file from source to destination.

C Sharp (C#)
File.[1]("source.txt", "dest.txt");
Drag options to blanks, or click blank then click option'
ADelete
BMove
CCopy
DExists
Attempts:
3 left
💡 Hint
Common Mistakes
Using Delete removes the file instead of copying.
Using Move moves the file instead of copying.
4fill in blank
hard

Fill both blanks to write text to a file, overwriting if it exists.

C Sharp (C#)
File.[1]("log.txt", [2]);
Drag options to blanks, or click blank then click option'
AWriteAllText
B"Hello, world!"
C"Sample text"
DAppendAllText
Attempts:
3 left
💡 Hint
Common Mistakes
Using AppendAllText adds text instead of overwriting.
Passing a variable name instead of a string literal.
5fill in blank
hard

Fill all three blanks to move a file only if it exists.

C Sharp (C#)
if (File.[1]("old.txt")) {
    File.[2]("old.txt", [3]);
}
Drag options to blanks, or click blank then click option'
AExists
BMove
C"new.txt"
DCopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using Copy instead of Move changes file behavior.
Not checking if file exists before moving.

Practice

(1/5)
1. Which of the following File class static methods checks if a file exists at a given path?
easy
A. File.Exists(path)
B. File.ReadAllText(path)
C. File.Delete(path)
D. File.Copy(source, destination)

Solution

  1. 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.
  2. Step 2: Identify the method that checks existence

    The method that returns a boolean indicating if the file exists is File.Exists(path).
  3. Final Answer:

    File.Exists(path) -> Option A
  4. Quick 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
A. File.WriteText(path, "Hello World");
B. File.WriteAllText(path, "Hello World");
C. File.Write(path, "Hello World");
D. File.WriteLine(path, "Hello World");

Solution

  1. Step 1: Recall the correct method name for writing text

    The File class uses WriteAllText to write all text to a file at once.
  2. Step 2: Check method signatures

    WriteText, Write, and WriteLine are not valid static methods of File class.
  3. Final Answer:

    File.WriteAllText(path, "Hello World") -> Option B
  4. Quick 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
A. Hello
B. test.txt
C. File.ReadAllText
D. Error: File not found

Solution

  1. Step 1: Understand what File.ReadAllText does

    This method reads all text from the specified file and returns it as a string.
  2. Step 2: Analyze the code output

    The variable content will hold "Hello" from the file. The Console.WriteLine prints this string.
  3. Final Answer:

    Hello -> Option A
  4. Quick 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
A. No error, code runs fine
B. File.Delete should be called before the first File.Copy
C. File.Copy cannot copy files with .txt extension
D. Second File.Copy will throw an exception because source.txt was deleted

Solution

  1. 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.
  2. Step 2: Identify the problem

    After deletion, source.txt no longer exists, so the second copy call will throw a FileNotFoundException.
  3. Final Answer:

    Second File.Copy will throw an exception because source.txt was deleted -> Option D
  4. Quick 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
A. if (File.Exists("file.txt")) File.Copy("file.txt", "backup.txt", true);
B. File.Copy("file.txt", "backup.txt");
C. if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt");
D. File.Copy("file.txt", "backup.txt", false);

Solution

  1. Step 1: Understand the requirements

    Backup only if original file exists and do not overwrite existing backup file.
  2. 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.
  3. Final Answer:

    if (File.Exists("file.txt") && !File.Exists("backup.txt")) File.Copy("file.txt", "backup.txt"); -> Option C
  4. Quick 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