5

I have to save files in my dotnet core application. I originally developed in windows; but when I ported the code to try developing on a mac, the file path is no longer valid (i.e. "c:\content..."). Is there a way to reference a file path, in my code, that will work in both situations? We deploy to azure... so the windows file path must there too.

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

1 Answers1

10

Use Path.Combine method like this:

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

Code from https://github.com/tomaszzmuda/Xabe.FFmpeg/blob/c8cc4232b5afa2860ede3be63a680d754ed73002/Xabe.FFmpeg.Test/Resources.cs

Tomasz Żmuda
  • 629
  • 6
  • 9
  • Sorry for the dig but it appears that `Environment.CurrentDirectory` returns `/`. I guess it is because the app is ran by visual studio in `./bin/Debug/net47/MyApp.app`, MyApp being the app package, so when the app is launched, the current dir is `/` ? – Sw0ut Oct 02 '19 at 13:05
  • 1
    If you want to get current directory look at this:https://stackoverflow.com/questions/38071639/c-sharp-get-assembly-executable-directory?rq=1 – Tomasz Żmuda Oct 02 '19 at 17:53