简体   繁体   English

C# Linux/MacOS 上的绝对文件路径

[英]C# Absolute File Path on Linux/MacOS

Consider the following code:考虑以下代码:

byte[] bytes = await File.ReadAllBytesAsync("~/Desktop/data.bin");

This throws the following error:这会引发以下错误:

Could not find a part of the path '/Users/mainuser/Desktop/DataExample/DataExample/bin/Debug.net5.0/~/Desktop/data.bin'.找不到路径“/Users/mainuser/Desktop/DataExample/DataExample/bin/Debug.net5.0/~/Desktop/data.bin”的一部分。

Why is the path not considered absolute, and how do I fix this?为什么路径不被视为绝对路径,我该如何解决这个问题?

Note: I'm running this on MacOS, but the same (presumably) applies to Linux.注意:我在 MacOS 上运行它,但同样(大概)适用于 Linux。

I assume you can try Environment.GetFolderPath(Environment.SpecialFolder.Desktop) ?我假设您可以尝试Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

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

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