.NET Core 中的跨平台文件名处理 [英] Cross-platform file name handling in .NET Core

查看:32
本文介绍了.NET Core 中的跨平台文件名处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以跨平台的方式处理System.IO类中的文件名,使其在Windows和Linux上都能正常工作?

例如,我编写的这段代码在 Windows 上运行良好,但它不会在 Ubuntu Linux 上创建文件:

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

解决方案

Windows 使用反斜杠.使用斜线的 Linux.Path.Combine 设置正确的符号 :
Path.Combine 方法 - MSDN

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

这篇关于.NET Core 中的跨平台文件名处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆