2012-06-08 92 views
0

我正在從Internet讀取XML文件。我將其寫入獨立存儲,我喜歡讀它之後that.Here是代碼IsolatedStorageFileStream不允許操作

IsolatedStorageFile isf=IsolatedStorageFile.GetUserStoreForApplication(); 
    private void findNearestButton_Click(object sender, RoutedEventArgs e) 
    { 

     findCity(isf); 
    } 

    private void findCity(IsolatedStorageFile isf) 
    { 
     filePath = AppResource.exchangeOfficesFile; 

      if (!isf.FileExists(filePath.ToString())) 
      { 
       takeXMLOnLine(isf,filePath); 
      } 
      parseXMLfile(isf,filePath); 

    } 



private void takeXMLOnLine(IsolatedStorageFile isf, string filePath) 
    { 
     System.Uri targetUri = new System.Uri(AppResource.exchangeOfficesURI); 
     WebClient client = new WebClient(); 
     client.DownloadStringAsync(targetUri); 
     client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); 

    } 



void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     using (IsolatedStorageFileStream rawStream = isf.OpenFile(filePath, FileMode.Create)) 
     { 
      XmlWriterSettings settings = new XmlWriterSettings(); 
      settings.ConformanceLevel = ConformanceLevel.Auto; 
      using (XmlWriter writer = XmlWriter.Create(rawStream, settings)) 
      { 
       string xmlResponse = e.Result.ToString(); 
       xmlResponse = xmlResponse.Replace("&lt;", "<"); 
       xmlResponse = xmlResponse.Replace("&gt;", ">"); 
       writer.WriteString(xmlResponse); 
       // Write the XML to the file. 
       writer.Flush(); 
      } 

     } 
    } 


private void parseXMLfile(IsolatedStorageFile isf, string filePath) 
    { 

      using (IsolatedStorageFileStream str = isf.OpenFile(filePath, FileMode.Open, FileAccess.Read)) 
      { 
       StreamReader reader = new StreamReader(str); 
       string line; 


       while ((line = reader.ReadLine()) != null) 
       { 

        MessageBox.Show(line); 


       } 
       reader.Close(); 
      } 

}

當我運行代碼,我得到操作不允許在IsolatedStoreageFileStream錯誤在該行

using (IsolatedStorageFileStream str = isf.OpenFile(filePath, FileMode.Open, FileAccess.Read)) 

有人能幫我嗎?

+0

有已經與存在該名稱的文件,在獨立存儲? –

+0

不,沒有。我用一個文件名運行我的應用程序,並且!fileExist()始終爲真。 – vikifor

回答

0

嘗試這樣:

using (IsolatedStorageFile rootStore = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream(filePath, 
        System.IO.FileMode.Open, rootStore)) 
    { 
     ... whatever you do with the file goes here 
    } 
} 
0

如果文件路徑有一個子目錄,看是否該目錄存在。這可能會導致這個問題。

+0

這不是我在IsolatedStorage – vikifor

0

當您嘗試讀取文件時文件是否存在?它看起來像文件不存在,你開始下載過程(但不要等待完成),然後嘗試讀取它。

因此,不是,是這樣的:

private void findCity(IsolatedStorageFile isf) 
{ 
    filePath = AppResource.exchangeOfficesFile; 

    if (!isf.FileExists(filePath.ToString())) 
    { 
     takeXMLOnLine(isf,filePath); 
    } 
    else 
    { 
     parseXMLfile(isf,filePath); 
    } 
} 

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
    using (IsolatedStorageFileStream rawStream = isf.OpenFile(filePath, FileMode.Create)) 
    { 
     XmlWriterSettings settings = new XmlWriterSettings(); 
     settings.ConformanceLevel = ConformanceLevel.Auto; 
     using (XmlWriter writer = XmlWriter.Create(rawStream, settings)) 
     { 
      string xmlResponse = e.Result.ToString(); 
      xmlResponse = xmlResponse.Replace("&lt;", "<"); 
      xmlResponse = xmlResponse.Replace("&gt;", ">"); 
      writer.WriteString(xmlResponse); 
      // Write the XML to the file. 
      writer.Flush(); 
     } 
    } 

    parseXMLfile(isf,filePath); 
} 
+0

創建它的子目錄中它總是說,該文件不存在,並嘗試重新下載它,並有ObjectDisposeException了未處理創建文件... – vikifor

+0

如果我使用SteamWriter沒有的XmlWriter它的工作原理但在這條路上 – vikifor

+0

我總是檢查上按一下按鈕,如果該文件存在。如果該文件不存在,我用一個函數來下載it.After我稱之爲功能從文件中讀取。問題是我通常在IsoaltedStorageFileStream異常中獲取操作不允許。我刪除功能從讀碼和第一隻調用了寫作,並運行該程序沒有任何exception.Then我再次寫了閱讀的功能和它的作品。爲什麼第一種方式不起作用? – vikifor

相關問題