2012-06-18 134 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