2015-07-19 34 views
0

在C#中使用Android應用程序。每次調用onCreate()時嘗試訪問和編輯文件。 (在OnCreate()調用updateTileInstallationStatus())OnCreate()中的Xamarin中的「在路徑上共享衝突...」

  private void updateTileInstallationStatus() 
      { 

       using (StreamReader streamReader = new StreamReader(Assets.Open("tileData.xml"))) { 
          XmlDocument xmlDoc = setAllTilesUninstalled(streamReader); 
          //some file editing here... 
          xmlDoc.Save(getFilePath()); 
       } 
      } 

      private XmlDocument setAllTilesUninstalled(StreamReader streamReader) 
      { 
       //use template from Assets to set all tile installation status as false 
       XmlDocument xmlDoc = new XmlDocument(); 
       xmlDoc.Load (streamReader); 
       xmlDoc.Save(getFilePath()); 
       return xmlDoc; 
      } 

它成功地保存該文件第一次應用是水合,但在導航離開後重新打開應用程序拋出一個「共享衝突的道路......」。

在其他文章中,它看起來像通過關閉StreamReader解決了問題。我嘗試了在updateTile ...()中保存調用後執行此操作,但這並沒有解決問題。與使用聲明相同的東西。看起來流沒有被關閉,然後updateTile ..()第二次被調用時,它不能訪問文件,因爲它被鎖定了?

有關訪問/編輯文檔或關閉流的更好方法的任何建議?

+0

是從源文件讀取還是寫入目標文件時發生的共享衝突? – Jason

+0

對xmlDoc.save(...)的調用發生共享衝突。它在前兩次調用時不拋出違規(一次在updateTiles中,另一次在setAllTiles中,但它是作爲參數傳遞的同一個xmlDoc對象)。在創建新的xmlDocument對象並嘗試保存後,保存調用拋出異常。 – merlam

回答

0

解決:從另一個方法,我從來沒有關閉,這阻止了我從保存的開放流。