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

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

问题描述

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

例如,我编写了这段代码,该代码在Windows上可以完美运行,但是在Ubuntu Linux上却无法创建文件:

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

解决方案

使用反斜杠的Windows. Linux使用Slash. 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 = $@"..\Data\uploads\{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天全站免登陆