简体   繁体   English

.NET Core 中的跨平台文件名处理

[英]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?如何跨平台处理System.IO类中的文件名,使其在 Windows 和 Linux 上工作?

For example, I write this code that works perfectly on Windows, however it doesn't create a file on Ubuntu Linux:例如,我编写的这段代码可以在 Windows 上完美运行,但是它不会在 Ubuntu Linux 上创建文件:

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

You can also use Path.DirectorySeparatorChar as below:您还可以使用 Path.DirectorySeparatorChar 如下:

 Console.WriteLine("..{0}Data{0}uploads{0}{{filename}}", Path.DirectorySeparatorChar);

Reference: MSDN参考: MSDN

Windows using Backslash. Windows 使用反斜杠。 Linux using Slash. Linux 使用斜线。 Path.Combine set the right symbol : Path.Combine 设置正确的符号:
Path.Combine Method - MSDN Path.Combine 方法 - MSDN

Lots of good answers.很多好的答案。 I would just like to add that one can avoid having to specify the directory separator character by using Path.Combine我只想补充一点,可以避免使用 Path.Combine 指定目录分隔符

Example with the file relatively located at ".\\..\\toto\\app.config":文件相对位于“.\\..\\toto\\app.config”的示例:

Path.Combine("..", "toto", "app.config");

Unfortunately, Path.Combine does not resolve a relative path to an absolute Path in .Net Core.不幸的是,Path.Combine 没有将相对路径解析为 .Net Core 中的绝对路径。 Path.GetFullPath is here for that: Path.GetFullPath 在这里:

Path.GetFullPath(Path.Combine("..", "toto", "app.config"))

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)相对路径的工作方式相同,绝对路径只能相对于主驱动器的根目录(因为以“c:”开头的绝对路径不可移植)

The original post is over a year old but I still ran into this issue.原来的帖子已经一年多了,但我仍然遇到了这个问题。 It seems to me like the use of dots in relative paths is also an issue.在我看来,在相对路径中使用点也是一个问题。

A path like一条像

".\\input\\mydata.csv" 

worked well on windows but not on unix.在 windows 上运行良好,但在 unix 上运行良好。 When changing the dot-notation for current directory to:将当前目录的点符号更改为:

Path.GetFullPath(Directory.GetCurrentDirectory())

I had more success.我取得了更大的成功。

Of course forward slashes work fine - except when they don't .当然,正斜杠工作正常 - 除非他们没有 It is an older problem, but certainly LoadLibrary actually bit me in this regard.这是一个较老的问题,但当然 LoadLibrary 在这方面确实咬了我。 Please see https://stackoverflow.com/a/34708551/1318024 which discusses this.请参阅讨论此问题的https://stackoverflow.com/a/34708551/1318024 Even though we do expect Windows to handle path separators gracefully (which we do not expect for *nix!) it is best to use the path separator appropriate for the platform.尽管我们确实希望 Windows 能够优雅地处理路径分隔符(我们希望 *nix!),但最好使用适合平台的路径分隔符。

There are also problem with file name cases in linux. linux中文件名大小写也有问题。 For instance if you have file name like Index.js and used in your code like index.js vice versa, you are having a problem too例如,如果你有像 Index.js 这样的文件名并在你的代码中使用像 index.js 反之亦然,那么你也有问题

I do development on windows and linux so relative path settings in json config are not always correct for the platform.我在 windows 和 linux 上进行开发,所以 json 配置中的相对路径设置对于平台来说并不总是正确的。 Path.Combine doesn't help if you have a path separator in the config path.如果配置路径中有路径分隔符,则 Path.Combine 无济于事。 Using a Replace does the trick tho.使用 Replace 可以解决问题。 Eg:例如:

var root = "c:\\bob";
var dir = "somepath/fred";
var path = Path.Combine(root, dir);  // = c:\bob\somepath/fred

path.Replace('/', Path.DirectorySeparatorChar); // = c:\fred\somepath\fred on windows
// For Windows
public static char slashSeparator = Path.AltDirectorySeparatorChar;
// For Linux
// public static char slashSeparator = Path.DirectorySeparatorChar;

var dirPath = $"{slashSeparator }dir1{slashSeparator }dir2{slashSeparator }";

Microsoft Documentation 微软文档

you can use this simple function:你可以使用这个简单的 function:

private string get_platform_compatible_path(string path)
{
   return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM