Deleting files in a cross platform compatible manner in .NET Core

Viewed 3239

I would like to delete a file in a .NET Core 2.0 application but cannot find any alternative to File.Delete.

When creating or reading files we are encouraged to use providers as below because they wrap the underlying file system allowing cross platform access:

   IFileProvider provider = new PhysicalFileProvider(dir);
   IFileInfo fileInfo = provider.GetFileInfo(fileName); 

However there does not appear to be any complimentary functionality for delete operations. System.IO is available in .NET Core 2.0 but is this cross platform compatible?

1 Answers

I just tested this trivial app on Ubuntu 16.04 and it worked.

using System;
using System.IO;

namespace gbsills
{
    class Program
    {
        static void Main(string[] args)
        {
            File.Delete("file.txt");
        }
    }
}

Of course, you'll need to make sure you use the correct file path separators and such if you want to be cross-platform.

Related