2017-07-11 36 views
0
 var sdcardpath = Android.OS.Environment.ExternalStorageDirectory.Path; 
     var filepath = System.IO.Path.Combine(sdcardpath, "first.html"); 
     System.IO.StreamWriter writer = new StreamWriter(filepath, true); 
     if (!System.IO.File.Exists(filepath)) 
     { 
      writer.Write(htmltext); 
     } 
     else 
     { 
      var txt = System.IO.File.ReadAllText(filepath); 
     } 

這樣,我想從我的本地存儲讀取HTML,但同時readalltext我得到異常 System.IO.IOException:在路徑共享衝突/storage/emulated/0/first.html如何從本地存儲中讀取xamarin android?

+0

你爲什麼要創建一個作家,然後讀取文件中的StreamWriter創造?爲什麼不把作者放在if區塊? – Jason

+0

謝謝你傑森你的信息 – Nawin

回答

0

當你這樣做

System.IO.StreamWriter writer = new StreamWriter(filepath, true); 

它打開/創建一個文件在filepath。所以在這之後,文件總是存在的(假設路徑是正確的並且允許權限)。然後你嘗試閱讀它,但是你已經寫好了,所以這是不允許的。

如果你想查看該文件是否存在,如果沒有,寫它,然後將檢查

if (!System.IO.File.Exists(filepath)) 
{ 
    using (var writer = new StreamWriter(filepath, true)) 
    { 
     writer.Write(htmltext); 
    } 
} 
else 
{ 
    var txt = System.IO.File.ReadAllText(filepath); 
} 
+0

我明白了......但還有一個問題是,一旦我再次打開文本到這個文件,我再次打開空文件,我該如何糾正 – Nawin

相關問題