Directories in C#

Here’s the translation of the Go code to C# with explanations in Markdown format suitable for Hugo:

Our program demonstrates how to work with directories in C#. Here’s the full source code:

using System;
using System.IO;

class Program
{
    static void Check(Exception e)
    {
        if (e != null)
        {
            throw e;
        }
    }

    static void Main()
    {
        // Create a new sub-directory in the current working directory.
        try
        {
            Directory.CreateDirectory("subdir");
        }
        catch (Exception e)
        {
            Check(e);
        }

        // When creating temporary directories, it's good practice to clean them up afterwards.
        // We'll use a try-finally block to ensure cleanup.
        try
        {
            // Helper function to create a new empty file.
            void CreateEmptyFile(string name)
            {
                File.WriteAllText(name, "");
            }

            CreateEmptyFile("subdir/file1");

            // We can create a hierarchy of directories, including parents with CreateDirectory.
            // This is similar to the command-line `mkdir -p`.
            try
            {
                Directory.CreateDirectory("subdir/parent/child");
            }
            catch (Exception e)
            {
                Check(e);
            }

            CreateEmptyFile("subdir/parent/file2");
            CreateEmptyFile("subdir/parent/file3");
            CreateEmptyFile("subdir/parent/child/file4");

            // GetDirectories and GetFiles list directory contents.
            var entries = Directory.GetFileSystemEntries("subdir/parent");

            Console.WriteLine("Listing subdir/parent");
            foreach (var entry in entries)
            {
                var info = new FileInfo(entry);
                Console.WriteLine($"  {info.Name} {info.Attributes.HasFlag(FileAttributes.Directory)}");
            }

            // Change the current working directory, similarly to `cd`.
            Directory.SetCurrentDirectory("subdir/parent/child");

            // Now we'll see the contents of `subdir/parent/child` when listing the current directory.
            entries = Directory.GetFileSystemEntries(".");

            Console.WriteLine("Listing subdir/parent/child");
            foreach (var entry in entries)
            {
                var info = new FileInfo(entry);
                Console.WriteLine($"  {info.Name} {info.Attributes.HasFlag(FileAttributes.Directory)}");
            }

            // Change back to where we started.
            Directory.SetCurrentDirectory("../../..");

            // We can also visit a directory recursively, including all its sub-directories.
            Console.WriteLine("Visiting subdir");
            Visit("subdir");
        }
        finally
        {
            // Clean up: delete the whole directory tree.
            Directory.Delete("subdir", true);
        }
    }

    // Visit is called for every file or directory found recursively.
    static void Visit(string path)
    {
        foreach (var entry in Directory.GetFileSystemEntries(path))
        {
            var info = new FileInfo(entry);
            Console.WriteLine($"  {info.FullName} {info.Attributes.HasFlag(FileAttributes.Directory)}");

            if (info.Attributes.HasFlag(FileAttributes.Directory))
            {
                Visit(entry);
            }
        }
    }
}

To run the program, save it as Directories.cs and use the C# compiler to build and run it:

$ csc Directories.cs
$ mono Directories.exe
Listing subdir/parent
  child True
  file2 False
  file3 False
Listing subdir/parent/child
  file4 False
Visiting subdir
  subdir\file1 False
  subdir\parent True
  subdir\parent\child True
  subdir\parent\child\file4 False
  subdir\parent\file2 False
  subdir\parent\file3 False

This program demonstrates various operations with directories in C#, including creating directories, listing contents, changing the current directory, and recursively visiting a directory tree. It uses the System.IO namespace which provides classes for working with directories and files.

The Directory class is used for most directory operations, while File is used for file operations. The FileInfo class provides detailed information about files and directories.

Note that in C#, we use try-catch blocks for error handling instead of the check function used in the original example. Also, C# uses garbage collection, so we don’t need to explicitly defer cleanup operations. However, we still use a try-finally block to ensure the temporary directory is deleted even if an exception occurs.

The Visit method demonstrates how to recursively traverse a directory structure in C#. It’s not as concise as the filepath.WalkDir function in the original example, but it provides similar functionality.