File System: Directories, DirectoryInfo, and List Files Program

File System: Directories, DirectoryInfo, and List Files Program

  • Purpose of the file system discussion: understanding how to interact with directories and files to manage a collection of documents stored on the system. We focus on the directory system as a way to organize and access files and subdirectories.

  • Language and type system support (in this context):

    • Directory type provides a collection of standard methods (static methods) for interacting with the file system, including creating and reading directory information.
    • DirectoryInfo type gives information about a specific directory and the files and subdirectories it contains.
    • FileInfo type provides information about files (metadata) but does not give access to the file contents.
    • FileInfo details include: size, full name, extension, name part without the extension, and attributes such as Readable, Write, and Archive bits.
  • Environment class and current working directory:

    • A property named CurrentDirectory (in the Environment class) returns the present working directory as a string.
    • This is useful when you need to know or display where your program is currently operating in the file system.
  • Example program: List Files

    • Purpose: list information about all files and directories in the current working directory.
    • High-level steps:
    • Display the name of the current working directory.
    • Obtain a DirectoryInfo object for the current working directory.
    • Use the DirectoryInfo object to get a list of FileInfo objects (for files) and DirectoryInfo objects (for subdirectories).
    • Iterate over these lists with foreach loops to display their information.
  • Practical setup and environment details:

    • Development environment: Visual Studio.
    • Namespace requirement: to work with DirectoryInfo and FileInfo, include the System.IO namespace (using System.IO;).
    • The current working directory is retrieved as a string (e.g., pwd) via Environment.CurrentDirectory.
    • String interpolation is used to emit output, e.g.:
    • The current working directory is {pwd}
    • When running in a debugging session, the program often runs from a bin/Debug (or similar) folder, not the source file directory. This explains why the actual working directory printed may be a path like …\bin\Debug...
  • Observations about path representation during debugging:

    • In the debugger, the current directory may show escaped backslashes in some views, but at runtime the path uses single backslashes (Windows style).
    • You may need to adjust the working directory in your project properties (under Debug) to point to the directory you want to inspect.
  • Required API usage and behavior:

    • Add a using directive for the IO namespace: using System.IO; to access DirectoryInfo and FileInfo.
    • DirectoryInfo provides location metadata: creation time, parent directory, root directory, etc.
    • DirectoryInfo and FileInfo expose properties for metadata, but not file contents.
    • DirectoryInfo.GetFiles() has multiple overloads to retrieve files in the directory; similarly, DirectoryInfo.GetDirectories() retrieves subdirectories.
  • Building the list: Directory, Files, and Subdirectories

    • Start with a DirectoryInfo for the current directory (di).
    • Get files: FileInfo[] files = di.GetFiles();
    • Get subdirectories: DirectoryInfo[] subdirs = di.GetDirectories();
    • For each file, display:
    • Name (file.Name)
    • Size (file.Length in bytes)
    • Creation time (file.CreationTime)
    • For each subdirectory, display its information similarly (but note that subdirectories themselves are DirectoryInfo objects).
  • Output formatting considerations:

    • To align output nicely, format columns with fixed widths, for example:
    • Name in a field width of 25 characters: {0,-25}
    • Size in a field width of 10 characters: {1,10}
    • Creation time as a date/time value: {2}
    • This is typically done via a composite format string or string interpolation, e.g.:
    • Console.WriteLine("{0,-25} {1,10} {2}", file.Name, file.Length, file.CreationTime);
    • The transcript mentions expanding the width if file names are long (e.g., widening from 25 to 35 characters).
    • The default display for DateTime shows a date followed by time in hours, minutes, and seconds; however, the DateTime type can represent more precise timing if needed (e.g., milliseconds, etc.). In the transcript, there is mention that milliseconds and microseconds can be used if chosen.
  • Example output (from the transcript):

    • Files listed in the directory (example files):
    • ListFiles.dll (178 bytes) created on 03/28/202108:06:51PM03/28/2021 \, 08:06:51 PM
    • ListFiles.exe (781 bytes) created on 03/28/202108:06:51PM03/28/2021 \, 08:06:51 PM
    • ListFiles.csproj (178 bytes) created on 03/28/202108:06:51PM03/28/2021 \, 08:06:51 PM
    • Output shows Name, Size, and CreationTime values, formatted with the specified widths.
    • The program can also enumerate directories in the current directory, such as subfolders (e.g., bin, obj, Properties) when listing directories.
  • Observations about debugging and debugging-time behavior:

    • In a debugging session, the project’s working directory may be different from the source folder. You may need to set the working directory explicitly in the project’s Debug settings to a directory such as the one containing the files you want to inspect (e.g., the ListFiles directory).
    • After listing, you may switch back and forth between the Locals window and the code to inspect DirectoryInfo and FileInfo properties (e.g., Parent, Root).
    • If you introduce a breakpoint (e.g., after obtaining the DirectoryInfo), you can inspect the metadata (e.g., the directory's creation time), and then continue execution after removing or disabling the breakpoint.
  • Practical capabilities and limitations:

    • The List Files program demonstrates reading directory metadata and formatting it for display, but it does not read file contents.
    • You can perform actions on files and directories, such as deleting a directory or reading metadata from its children, but the transcript notes that some actions (like deleting) were not performed in the example.
  • Case study and extension ideas mentioned in the talk:

    • Using the DateTime values (LastAccessTime, CreationTime) in further code (e.g., to sort or filter files by age).
    • Exploring the properties exposed by DirectoryInfo, such as Parent and Root, to navigate the file system.
    • Using GetFiles with different overloads to filter by search patterns or search options.
  • Summary of key concepts and terminology:

    • Directory: a collection of standard static methods used to interact with the file system.
    • DirectoryInfo: metadata about a particular directory, including contained files and subdirectories.
    • FileInfo: metadata about a file (size, name, extension, attributes) without the contents.
    • Environment.CurrentDirectory: the present working directory as a string.
    • List Files program: demonstrates combining DirectoryInfo.GetFiles(), DirectoryInfo.GetDirectories(), and formatting to display a directory’s contents.
    • DateTime: type used for time-related information (CreationTime, LastAccessTime) and its default print format; supports more precise timing if needed.
  • Quick reference (conceptual):

    • Get current directory:
    • extpwd=extEnvironment.CurrentDirectoryext{pwd} = ext{Environment.CurrentDirectory}
    • Retrieve file list:extvarfiles=extdi.GetFiles()ext{var files} = ext{di.GetFiles()}
    • Retrieve subdirectories:extvardirs=extdi.GetDirectories()ext{var dirs} = ext{di.GetDirectories()}
    • Display format (examples):
    • extNameoextwidth25,extSizeoextwidth10,extCreationTimeoextDateTimevalueext{Name} o ext{width } 25, ext{ Size} o ext{width } 10, ext{ CreationTime} o ext{DateTime value}
    • DateTime default display (for a value like CreationTime):extdateextandexttime(hours:minutes:seconds)ext{date} ext{ and } ext{time (hours:minutes:seconds)}
  • Notes for exam preparation:

    • Remember the distinction between DirectoryInfo vs FileInfo and what each exposes.
    • Understand how to get the current directory and how to enumerate files and subdirectories.
    • Be comfortable with formatting output to align columns using fixed widths and with displaying DateTime values.
    • Be aware of how the debugging environment can affect the current working directory and how to set it if needed.