What should you do?

Domain.com has asked you to create an application to display all of the top directories based on the drive path.
You need to ensure that the application displays the number of files within top-level directories.
What should you do?

Domain.com has asked you to create an application to display all of the top directories based on the drive path.
You need to ensure that the application displays the number of files within top-level directories.
What should you do?

A.
Use the following code:
public void DisplayDriveDirectories (string drivePath)
{ if (Directory.Exists(drivePath)){
foreach (String dirPath in Directory.GetDirctories(drivePath)){
DirectoryInfo dir = new DirectoryInfo (drivePath);
int numFiles = dir.TotalFiles;
Console.WriteLine( “{0} : {1} files.”, dir.Name, numFiles); }
}
}

B.
Use the following code:
public void DisplayDriveDirectories (string drivePath)
{ if (Directory.Exists (drivePath)){
foreach (String dirPath in Directory.GetDirctories (drivePath)){
DirectoryInfo dir = new DirectoryInfo(drivePath);
int numFiles = dir.Length;
Console.WriteLine( “{0} : {1} files.”, dir.Name, numFiles); }
}
}

C.
Use the following code:
public void DisplayDriveDirectories (string drivePath)
{ if (Directory.Exists(drivePath)){
foreach (String dirPath in Directory.GetDirctories(drivePath)){
DirectoryInfo dir = new DirectoryInfo(drivePath);
int numFiles = dir.GetFiles().Length;
Console.WriteLine( “{0} : {1} files.”, dir.Name, numFiles); }
}
}

D.
Use the following code:
public void DisplayDriveDirectories (string drivePath)
{ if (Directory.Exists(drivePath)){
foreach (String dirPath in Directory.GetDirctories (drivePath)){
DirectoryInfo dir = new DirectoryInfo (drivePath);
int numFiles = dir.Size;
Console.WriteLine( “{0} : {1} files.”, dir.Name, numFiles); }
}
}

Explanation:
This code iterates through each top level of a given drive path and displays the Name property and number of files.
First, the Exists method verifies that the drive path exists. Then, the GetDirectories method is invoked and returns a string array of directory paths.
GetDirectories takes a directory path as an argument.
Then a DirectoryInfo object is instantiated using the dirPath variable as it is updated with each iteration.
The number of files in the directory is evaluated by using the GetFiles method, which returns an array of FileInfo objects and retrieves the Length Property of the array.
The value is assigned to the numFiles variable.
The DirectoryInfo object represents metadata about a directory instance. The Console.WriteLine method displays the Name property of the DirectoryInfo object and the numFiles variable.
Incorrect Answers:
A, B, D: The TotalFiles, Length, and Size properties do not exist in the DirectoryInfo class.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


seenagape

seenagape

I agree with the answer. C