Configuration Management

The find-visual-studio-orphaned-items application utilizes a configuration management system based on the Microsoft.Extensions.Configuration library. This system allows the application to load and manage settings from various sources, providing flexibility and extensibility in customizing application behavior.

Configuration Sources

The application supports loading configuration from the following sources in order of precedence:

  1. Command Line Arguments: Settings can be overridden directly via command-line arguments. This is the highest priority source, allowing for immediate changes during runtime.

    // Example: Overriding the "OutputDirectory" setting using the "-o" argument
              find-visual-studio-orphaned-items -o "C:\Temp\Output"
              
  2. Environment Variables: Configuration settings can be set as environment variables. This provides a platform-independent way to manage settings.

    // Example: Setting the "OutputDirectory" environment variable
              set OUTPUTDIRECTORY=C:\Temp\Output
              
  3. App Configuration File (appsettings.json): The application reads settings from a JSON file named appsettings.json located in the application’s root directory. This file provides a central location for storing default settings.

    {
                "OutputDirectory": "C:\\Temp\\Output",
                "Verbose": false
              }
              
  4. User Settings File (usersettings.json): The application stores user preferences in a JSON file named usersettings.json located in the user’s application data directory. This allows users to customize application settings on a per-user basis.

    {
                "OutputDirectory": "C:\\MyOutput",
                "Verbose": true
              }
              

Loading and Accessing Configuration

The application uses the IConfiguration interface to access configuration settings. The IConfiguration instance is created using the ConfigurationBuilder class, which allows you to add and configure the different sources.

The following code snippet demonstrates loading configuration from the various sources:

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory()) // Set the base path for the configuration files
              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // Add the app settings file
              .AddJsonFile("usersettings.json", optional: true, reloadOnChange: true) // Add the user settings file
              .AddEnvironmentVariables() // Add environment variables
              .AddCommandLine(args) // Add command-line arguments
              .Build();
          

Once the IConfiguration instance is created, you can access configuration values using the GetValue<T> method, as shown below:

// Access the "OutputDirectory" setting
          string outputDirectory = Configuration.GetValue<string>("OutputDirectory");
          
          // Access the "Verbose" setting
          bool isVerbose = Configuration.GetValue<bool>("Verbose");
          

Updating User Settings

The application allows users to modify their preferences through the user interface. These changes are then saved to the usersettings.json file.

The application uses the IConfigurationBuilder to reload the configuration from the usersettings.json file, ensuring that any changes made by the user are reflected in the application’s behavior.

Summary

The configuration management system provides a flexible and robust mechanism for managing application settings. It allows for easy customization of application behavior, both at runtime and through user preferences. The use of the Microsoft.Extensions.Configuration library ensures consistency and extensibility.