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 = $@"..\Data\uploads\{filename}"; using (FileStream fs = System.IO.File.Create(tempFilename)) { file.CopyTo(fs); fs.Flush(); }
NET Core is cross-platform. It runs on Windows, OS X and multiple distributions of Linux. It also supports different CPU architectures. We're adding more Linux distribution and CPU architecture support with the eventual goal of .
A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform.
C# path class comes under System.IO namespace and System. Runtime. dll assembly. This class is used to perform operations on string instances that have file path or directory path information. A path is a string that holds the location of the file or directory and it can be an absolute or relative location.
You can also use Path.DirectorySeparatorChar as below:
Console.WriteLine("..{0}Data{0}uploads{0}{{filename}}", Path.DirectorySeparatorChar);
Reference: MSDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With