2014-11-08 2 views
0

Я написал этот код в приложении Windows Phone для чтения и записи данных из XML-файла, и он отлично работает. поэтому я хочу использовать его в окнах 8.1 приложения, но это не работает, как я преобразовать его, чтобы быть совместимым с окнами 8.1Конвертировать IsolatedStorageFileStream из Windows phone 8.1 в Windows 8.1

public void Read(string strXMLFile) 
    { 
     IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication(); 
     XDocument doc = null; 
     IsolatedStorageFileStream isfStream = null; 
     if (isfData.FileExists(strXMLFile)) 
     { 
      isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData); 
      doc = XDocument.Load(isfStream); 
      isfStream.Close(); 
     } 
     else 
     { 
      doc = XDocument.Load(strXMLFile); 
      isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData); 
      doc.Save(isfStream); 
      isfStream.Close(); 
     } 

     var vData = from s in doc.Descendants("Row") select new ColData(s); 
    } 


    public void Write(string strXMLFile) 
    { 
     XElement xml = new XElement("Tabel", 
         from p in this 
         select p.Information); 

     IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication()); 
     xml.Save(isfStream); 
     isfStream.Close(); 

    } 
+0

Что именно вы имеете в виду, но «не работает»? –

+0

Я имею в виду, что этот код работает для телефона Windows 8.1. Приложение, но не работает для Windows 8.1 App или Windows RT App – masalahi

+0

да, но определите, что не работает. Исключения выбрасываются? Код висит? Что именно? –

ответ

0

Вы должны использовать новый API, чтобы прочитать файл

var file = await localFolder.GetFileAsync("dataFile.txt"); 
var data = await FileIO.ReadTextAsync(file); 

и писать

var file = await localFolder.CreateFileAsync("dataFile.txt",  CreateCollisionOption.ReplaceExisting); 
await FileIO.WriteTextAsync(file , "data"); 
0

член DamithSL ответить на вопрос на сайте проекта кода

http://www.codeproject.com/Questions/839861/Convert-IsolatedStorageFileStream-from-Windows-pho

public async Task Read(string fileName) 
    { 
     string text = ""; 
     IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; 

     IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName); 

     IRandomAccessStream accessStream = await storageFile.OpenReadAsync(); 

     using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size)) 
     { 

      byte[] content = new byte[stream.Length]; 

      await stream.ReadAsync(content, 0, (int)stream.Length); 

      text = Encoding.UTF8.GetString(content, 0, content.Length); 

     } 

     XDocument loadedDataH = XDocument.Parse(text); 
     var vPosting = from query in loadedDataH.Descendants("Row") 
         select new clssColPost(query); 
     this.Clear(); 
     AddRange(vPosting); 

    } 


    public async Task Write(string fileName) 
    { 

     XElement xml = new XElement("Tabel", 
          from p in this 
          select p.Information); 


     IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; 

     IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); 

     using (Stream stream = await storageFile.OpenStreamForWriteAsync()) 
     { 
      byte[] content = Encoding.UTF8.GetBytes(xml.ToString()); 
      await stream.WriteAsync(content, 0, content.Length); 
     } 
    }