[英]File access issue in C# application running on mac using mono
我開發了一個簡單的.net應用程序,該應用程序從Web上抓取一些數據並將其保存在一個文件中(該文件和proejct exe文件位於同一文件夾中)。
它在Windows上運行正常,但是當我在Mac上使用“單聲道”運行它時,出現此錯誤
拒絕訪問路徑“ [path]”。
我正在使用Directory.GetCurrentDirectory創建對該文件的訪問。
這是我正在使用的路徑:
System.IO.Directory.GetCurrentDirectory() + "\\abc.csv";
您不應該使用字符串來連接目錄,如果您希望應用程序是跨平台的,則不可以。
壞 :
Directory.GetCurrentDirectory() + "\\abc.csv";
好 :
Path.Combine(Directory.GetCurrentDirectory(), "abc.csv");
使用Path.Combine
將確保使用正確的目錄分隔符。
如果必須遍歷幾個目錄, Path.Combine
重復使用Path.Combine
:
Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "mydir"), "abc.csv");
可能是在Windows上使用了錯誤的斜杠,您可能會使用類似“ Path \\ to \\ my \\ file”之類的東西,但在Mac上,斜杠將被反轉為“ Path / to / my / file”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.