c# - System.IO.Path 或与 Unix 路径等效使用

标签 c# .net unix path

是否可以使用 System.IO.Path 类或一些类似的对象来格式化 unix 样式路径,从而提供与 PATH 类类似的功能?例如,我可以这样做:

Console.WriteLine(Path.Combine("c:\\", "windows"));

显示:

"C:\\windows"

但是如果我尝试用正斜杠 (/) 做类似的事情,它只会为我反转它们。

Console.WriteLine(Path.Combine("/server", "mydir"));

显示:

"/server\\mydir"

最佳答案

你遇到了更大的问题,Unix 接受文件名中的字符,而 Windows 不允许。此代码将用 ArgumentException 轰炸,“路径中的非法字符”:

  string path = Path.Combine("/server", "accts|payable");

您不能可靠地将 Path.Combine() 用于 Unix 路径。

关于c# - System.IO.Path 或与 Unix 路径等效使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2779807/

相关文章:

c# - "Cannot evaluate expression because the code of the current method is optimized."是什么意思?

.net - Server 2008 R2 上的 microsoft.interop.security.azroles 在哪里?

c - 如何中断输入

c - unix tcp 客户端/服务器奇怪的行为

c# - 为什么 TrackValue 支持 10 个维度值而 GetMetric 只支持 4 个维度名称?

c# - C#/.Net 中的高效多元线性回归

c# - 使用 Webbrowser C# 从 iframe 读取 HTML 代码

c# - Entity Framework - 一个 edmx 文件中的所有表

c# - 如何根据用户输入动态构建和返回 linq 谓词

linux - 如何让 Crontab 和 Tar 一起正确工作