When working with files in C#, we usually consider them to work correctly on all operating systems, as file paths differ between Windows, Linux, and macOS. For example, in Windows, we use "\" to separate directories, while in Linux, "/" is used. If we have specified paths in our code, we need to keep in mind that our code should work without errors and correctly on all operating systems. In this section, we focus on implementing a FileHelper that creates file paths based on the operating system on which the code is executed. For example, if it is executed in Windows, we would receive the following output:

.\Data\uploads\fileName.txt

And if the code is executed on Linux, we would receive the following output:

./Data/uploads/fileName.txt

In the first step, we might think of using a method to retrieve the file separator. However, a better solution that Microsoft [PathInternal.Unix.cs, PathInternal.Windows.cs and PathInternal.cs ] itself has used is as follows: Create a project of type "Shared" and create a folder named "Helpers". Inside that folder, create two additional files named "FileHelper.Windows.cs" and "FileHelper.Unix.cs". FileHelper.cs:
the FileHelper.Windows.cs file:

public static partial class FileHelper
{
    public const char DirectorySeparator = '\\';
}

the FileHelper.Unix.cs file:

public static partial class FileHelper
{
    public const char DirectorySeparator = '/';
}

Then, you need to include the code for including the Windows and Unix files in the "MultipleOsPlatform.Shared.projitems" file. The "MultipleOsPlatform.Shared.projitems" file should be structured as follows:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
    <HasSharedItems>true</HasSharedItems>
    <SharedGUID>f42755fb-0a29-4d81-bb93-4db1e4b32d4f</SharedGUID>
  </PropertyGroup>
  <PropertyGroup Label="Configuration">
    <Import_RootNamespace>MultipleOsPlatform.Shared</Import_RootNamespace>
  </PropertyGroup>
  <ItemGroup Condition="'$(OS)' == 'Unix'">
    <Compile Include="$(MSBuildThisFileDirectory)Helpers\FileHelper.Unix.cs" />
  </ItemGroup>
  <ItemGroup Condition="'$(OS)' == 'Windows_NT'">
    <Compile Include="$(MSBuildThisFileDirectory)Helpers\FileHelper.Windows.cs">
    </Compile>
  </ItemGroup>
</Project>

In the "MultipleOsPlatform.Shared.projitems" file, we specify that when the operating system is Unix, the "FileHelper.Unix.cs" file should be compiled, and when the operating system is Windows, the "FileHelper.Windows.cs" file should be compiled. This way, in the "DirectorySeparator" property, when the operating system is Linux, the character "/" is returned, and when the operating system is Windows, the character "\" is returned.

Next, create a Console Application and add the Shared project to its Shared References. Then, if you invoke the following code in the Console Application project:

var directorySeparator = FileHelper.DirectorySeparator;

Console.WriteLine(".{0}Data{0}uploads{0}fileName", directorySeparator);

In the Windows operating system, you would see the following output:

.\Data\uploads\fileName

And when executed on the Linux operating system, you would see the following output:

./Data/uploads/fileName

You can obtain the codes for this article from GitHub.

:)

Powered by Froala Editor

Comments