简体   繁体   中英

File access issue in C# application running on mac using mono

I developed a simple .net application that scrapes some data from web and saves it in a file (file and the proejct exe file are in the same folder ).

It runs fine on windows, but when i run it on mac using 'Mono' i get this error

Access to the path '[path]' is denied.

I am creating the accessing this file using Directory.GetCurrentDirectory.

here is the path i am using:

 System.IO.Directory.GetCurrentDirectory() + "\\abc.csv";

You should not concatenate directories using strings, not if you want your applications to be cross platform.

BAD :

Directory.GetCurrentDirectory() + "\\abc.csv";

Good :

Path.Combine(Directory.GetCurrentDirectory(), "abc.csv");

Using Path.Combine will ensure the correct directory separators are used.

If you have to traverse through several directories, use Path.Combine repeatedly:

Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "mydir"), "abc.csv");

可能是在Windows上使用了错误的斜杠,您可能会使用类似“ Path \\ to \\ my \\ file”之类的东西,但在Mac上,斜杠将被反转为“ Path / to / my / file”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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