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();
}
Windows using Backslash. Linux using Slash. Path.Combine set the right symbol :
Path.Combine Method - MSDN
You can also use Path.DirectorySeparatorChar as below:
Console.WriteLine("..{0}Data{0}uploads{0}{{filename}}", Path.DirectorySeparatorChar);
Reference: MSDN
You can simply use slashes. Relative paths will work identically, and absolute paths can only be relative to the root of the main drive (as absolute paths starting with "c:" are not portable)
链接地址: http://www.djcxy.com/p/54554.html上一篇: 如何在Python中获取主目录?
下一篇: .NET Core中的平台文件名称处理