Skip to content
Advertisement

Cross-platform file name handling in .NET Core

How to handle file name in System.IO classes in a cross-platform manner to make it work on Windows and Linux?

For example, I write this code that works perfectly on Windows, however it doesn’t create a file on Ubuntu Linux:

var tempFilename = $@"..Datauploads{filename}";
using (FileStream fs = System.IO.File.Create(tempFilename))
{
    file.CopyTo(fs);
    fs.Flush();                    
}

Advertisement

Answer

Windows using Backslash. Linux using Slash. Path.Combine set the right symbol :
Path.Combine Method – MSDN

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement