2012-06-18 136 views
0

當我試圖轉換成數據集的XML獨立存儲文件我得到這樣一個異常「無法訪問,因爲正在使用由其他用戶」XML轉換成數據集

我的代碼:

IsolatedStorageFile isfInsuranceFirm = null; 

isfInsuranceFirm = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null); 
Stream stream1 = new IsolatedStorageFileStream("PageAccess.xml",FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isfInsuranceFirm); 

stream1.Position = 0; 

string path1 = stream1.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream1).ToString(); 
string path2 = stream2.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream2).ToString(); 

XmlDataDocument doc = new XmlDataDocument(); 
//doc.LoadXml(path1.Substring(path1.IndexOf('/') + 1)); 
doc.Load(path1); 

回答

1

你似乎沒有妥善處理IDisposable資源。你應該總是把它們包裝在使用聲明,以確保你沒有泄漏手柄:

using (IsolatedStorageFile isfInsuranceFirm = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null)) 
using (Stream stream1 = new IsolatedStorageFileStream("PageAccess.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isfInsuranceFirm)) 
{ 

    stream1.Position = 0; 

    string path1 = stream1.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream1).ToString(); 
    string path2 = stream2.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream2).ToString(); 

    XmlDataDocument doc = new XmlDataDocument(); 
    //doc.LoadXml(path1.Substring(path1.IndexOf('/') + 1)); 
    doc.Load(path1); 
} 
+0

我試着如你所說,但同樣的錯誤發生 – sidd2004k