February 17, 2026

From ".slnx" => ".sln" file

To create a new ".slnx" solution file from a ".sln" file. Open a command window (cmd.exe). Within that window change to the folder containing your "{YourSolutionName}.sln" file. Type in the following line:

"dotnet sln {YourSolutionName}.sln migrate"
This generates a new "{YourSolutionName}.slnx" file. The original "{YourSolutionName}.sln" file remains untouched.

February 6, 2026

C# Dot Net SDK Version Macros

It is useful to be able to include or exclude code based on preprocessor macros. We can do this for the different versions of .NET

1. Use "#if" with framework version symbols to include specific code for specific .Net versions .NET SDK defines symbols like "Net5_0", "Net6_0", "Net7_0", etc. For .NET 10, the symbol will be "Net10_0". This works automatically if you're targeting .NET 10 via the SDK-style project and using the element <TargetFramework> like: "<TargetFramework>net10.0<TargetFramework>". Here is an example of this:

#if NET10_0
    // This code will be included when targeting .NET 10
    Console.WriteLine("Running on a framework other than .NET 10");
#endif
🧠 These symbols can be combined for example:

#if NET6_0 || NET7_0
    Console.WriteLine("Running on .NET 6 or 7");
#elif NET10_0
    Console.WriteLine("Running on .NET 10");
#endif
2. Invert the macro condition to exclude code using the ! sign For example, you can use "#if !NET10_0" to exclude code when the target is not .NET 10. Here is an example of this:

#if !NET10_0
    // This code will be excluded when targeting .NET 10
    Console.WriteLine("Running on a framework other than .NET 10");
#endif
3. You can also use a "_OR_GREATER" suffix, for example:

#if NET5_0_OR_GREATER
    // This code will be included when targeting .NET 5 or greater
#endif
There are also platform-specific preprocessor symbols for conditional compilation based on the operating system. These are especially useful when writing cross-platform code in .NET Core or .NET 5+. 🧭 Built-in platform symbols Here are the most common ones:
OS Target
WINDOWS Compiling for Windows
LINUX Compiling for Linux
OSX Compiling for Mac OS
ANDROID Compiling for Android
IOS Compiling for iOS
MACCATALYST Compiling for Mac Catalyst
FREEBSD Compiling for FreeBSD
BROWSER Compiling for WebAssembly (Blazor)
These are automatically defined by the SDK when targeting specific platforms.
✅ Example usage

#if WINDOWS
    Console.WriteLine("Running on Windows");
#elif LINUX
    Console.WriteLine("Running on Linux");
#elif OSX
    Console.WriteLine("Running on macOS");
#else
    Console.WriteLine("Unknown platform");
#endif

🔍 Notes

  • These symbols are only defined when the runtime identifier (RID) or target platform is specified appropriately in your ".csproj " file.
  • If you're multi-targeting or using runtime checks, you might prefer OperatingSystem.IsWindows() or similar APIs from System.Runtime.InteropServices

Note that this is heavily edited output from CoPilot.

January 30, 2026

Using System.IO.Abstractions for File System Abstraction in C#

System.IO.Abstractions is a library that helps abstract file system operations in C#. It is particularly useful when writing unit tests because it allows you to mock file system interactions. Here's how you can integrate it into your project for both regular file operations and testing scenarios.

1. Adding System.IO.Abstractions to Your Project

First, you'll need to add the System.IO.Abstractions NuGet package to your project. In your .csproj file, include the following line:

      <PackageReference Include="System.IO.Abstractions" Version="21.0.29" /> <!-- See https://github.com/TestableIO/System.IO.Abstractions -->

    

More details are here: https://github.com/TestableIO/System.IO.Abstractions

2. Registering the IFileSystem Service for Dependency Injection

Next, inject IFileSystem into your application's services. This allows you to easily work with the file system and mock it for testing. In your Startup.cs (or Program.cs), register the FileSystem implementation as a transient service:

      // Register IFileSystem to allow injection
services.AddTransient<IFileSystem, FileSystem>();

    

This ensures that the IFileSystem interface will be resolved to the concrete FileSystem class when injected.

3. Injecting and Using IFileSystem in Your Class

In your class, inject IFileSystem via the constructor. This allows you to interact with the file system in a testable way.

      public class SomeService
{
    private readonly IFileSystem _fileSystem;

    // Constructor injection of IFileSystem
    public SomeService(IFileSystem fileSystem)
    {
        _fileSystem = fileSystem;
    }

    // Example method using IFileSystem to check if a file exists
    public void DoSomething(string fileName)
    {
        bool exists = _fileSystem.File.Exists(fileName);
        if (exists)
        {
            var content = _fileSystem.File.ReadLines(fileName);
            // Process the content
			...
        }
    }
}

    

In this example, DoSomething checks if a file exists using the abstracted IFileSystem and reads the file's contents if it does.

4. Unit Testing with System.IO.Abstractions.TestingHelpers

For unit testing, you'll want to mock the file system to avoid interacting with the actual file system. You can do this by adding System.IO.Abstractions.TestingHelpers to your project:

      <PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="21.0.29" />

    

Now, you can use the MockFileSystem class to simulate a file system in your tests.

4.1. Mocking Files and Directories

Here's an extension method for setting up files (empty) and directories in your IFileSystem (mock or otherwise):

      public static IFileSystem AddFilesAndDirectories(
    this IFileSystem fileSystem, 
    IEnumerable<string> fileSystemEntries)
{
    if (fileSystem == null)
        throw new ArgumentNullException(nameof(fileSystem));

    if (fileSystemEntries == null)
        throw new ArgumentNullException(nameof(fileSystemEntries));

    foreach (var entry in fileSystemEntries.Select(x => x.Trim()))
    {
        // Check if the entry is a directory or file by checking the last character
        // if it is a directory separator assume it is a directory
        if (entry[^1] == Path.DirectorySeparatorChar || entry[^1] == Path.AltDirectorySeparatorChar)
        {
            fileSystem.AddDirectory(entry); // Add directory if the string ends with a directory separator
        }
        else // otherwise assume it is a file
        {
            fileSystem.AddFile(entry, "x"); // Add file with some content. File parent directories will also be created.
        }
    }

    return fileSystem;
}

    

This helper method adds directories and files to the MockFileSystem. It differentiates between files and directories based on the path format (whether it ends with a directory separator).

4.2. Example Unit Test

Here’s a simple unit test demonstrating how to use the MockFileSystem:

      using System.IO.Abstractions.TestingHelpers;
using Xunit;

public class SomeServiceTests
{
    [Fact]
    public void TestFileOperations()
    {
        // Setup mock file system
        var mockFileSystem = new MockFileSystem();

        var someService = new SomeService(fileSystem);
        ...
    }
}

    

You can use these methods to interact with files and directories in an abstracted way, making your code more testable and platform-independent.

6. Conclusion

System.IO.Abstractions is a library for abstracting file system operations, which simplifies unit testing and makes your code more modular and easier to maintain.

By using IFileSystem for dependency injection and MockFileSystem for unit tests, you can easily mock file system interactions without relying on the actual file system.By following the steps outlined above, you'll be able to set up file system abstractions in your project and write unit tests that are isolated from the underlying file system, improving the testability and reliability of your application.