【问题标题】:Code a File Path that works in both OSX and Windows (dotnet core)编写适用于 OSX 和 Windows(dotnet 核心)的文件路径
【发布时间】:2017-09-21 13:22:49
【问题描述】:

我必须将文件保存在我的 dotnet 核心应用程序中。我最初是在windows中开发的;但是当我移植代码尝试在 Mac 上开发时,文件路径不再有效(即“c:\content...”)。有没有办法在我的代码中引用文件路径,这两种情况都适用?我们部署到天蓝色...所以 windows 文件路径也必须在那里。

FileInfo fileInfo = new FileInfo("c:\\Content\\SalesOrderOutput\\" + 
      fileName.txt);
FileStream stream = fileInfo.OpenWrite();

【问题讨论】:

    标签: macos .net-core


    【解决方案1】:

    像这样使用 Path.Combine 方法:

    internal static readonly FileInfo Mp4WithAudio = new FileInfo(Path.Combine(Environment.CurrentDirectory, "Resources", "input.mp4"));
    

    来自https://github.com/tomaszzmuda/Xabe.FFmpeg/blob/c8cc4232b5afa2860ede3be63a680d754ed73002/Xabe.FFmpeg.Test/Resources.cs的代码

    【讨论】:

    • 抱歉挖掘,但似乎Environment.CurrentDirectory 返回/。我猜是因为应用程序是由./bin/Debug/net47/MyApp.app的visual studio运行的,MyApp是应用程序包,所以当应用程序启动时,当前目录是/
    • 如果你想获取当前目录看这个:stackoverflow.com/questions/38071639/…
    猜你喜欢
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多