0
0
C Sharp (C#)programming~10 mins

File paths and Directory operations in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the file name from the full path.

C Sharp (C#)
string filePath = @"C:\Users\Public\Documents\report.pdf";
string fileName = System.IO.Path.[1](filePath);
Console.WriteLine(fileName);
Drag options to blanks, or click blank then click option'
AGetDirectoryName
BGetFileName
CCombine
DGetExtension
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetDirectoryName returns the folder path, not the file name.
Using GetExtension returns only the file extension.
2fill in blank
medium

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

C Sharp (C#)
string dirPath = @"C:\Temp\Logs";
bool exists = System.IO.Directory.[1](dirPath);
Console.WriteLine(exists);
Drag options to blanks, or click blank then click option'
ACreateDirectory
BGetFiles
CExists
DDelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using CreateDirectory will create the directory instead of checking.
Using GetFiles returns files inside the directory, not existence.
3fill in blank
hard

Fix the error in the code to combine two parts of a file path correctly.

C Sharp (C#)
string folder = @"C:\Users\Public";
string file = "data.txt";
string fullPath = System.IO.Path.[1](folder, file);
Console.WriteLine(fullPath);
Drag options to blanks, or click blank then click option'
AConcat
BJoin
CAppend
DCombine
Attempts:
3 left
💡 Hint
Common Mistakes
Using Join or Concat does not handle path separators correctly.
Append is not a method of Path.
4fill in blank
hard

Fill both blanks to create a dictionary with file names as keys and their extensions as values from a list of file paths.

C Sharp (C#)
var files = new List<string> { @"C:\Docs\file1.txt", @"D:\Music\song.mp3" };
var fileDict = files.ToDictionary(f => System.IO.Path.[1](f), f => System.IO.Path.[2](f));
Drag options to blanks, or click blank then click option'
AGetFileName
BGetExtension
CGetDirectoryName
DCombine
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetDirectoryName instead of GetFileName for keys.
Using Combine instead of GetExtension for values.
5fill in blank
hard

Fill the blanks to create a dictionary with uppercase directory names as keys and the count of files inside as values.

C Sharp (C#)
var dirs = new List<string> { @"C:\Temp", @"D:\Data" };
var dirInfo = dirs.ToDictionary(d => System.IO.Path.[1](d).ToUpper(), d => System.IO.Directory.[2](d).Length);
Drag options to blanks, or click blank then click option'
AGetDirectoryName
BGetFiles
CGetDirectories
DGetFileName
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetDirectoryName instead of GetFileName for folder name.
Using GetDirectories instead of GetFiles for counting files.