2014-03-29 50 views
1

我有一個SQLite數據庫文件,我需要複製到將使用它的平臺,我有點東西。我已經看過如何從XML文件中讀取文本的示例,但我只需要獲取流並將其複製,但我找不到需要使用哪個類來完成此操作。從便攜式類庫複製文件到Windows 8 - SQLite數據庫

這是我到目前爲止有:

public async Task CopyDatabase() 
    { 
     var s = Assembly.Load(new AssemblyName("PhenotypesLibrary")).GetManifestResourceStream(DBPath.DBPathAsString()); 

     ////create a folder and file 
     var folder = ApplicationData.Current.LocalFolder; 
     var file = await folder.CreateFileAsync(DBPath.DBPathAsString()); 


     using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) 
     { 
      using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0)) 
      { 
       using (StreamWriter writer = new StreamWriter(outputStream.AsStreamForWrite())) 
       { 
        await writer.//ummmmmmmmm 
       } 
       //using (DataWriter dataWriter = new DataWriter(outputStream)) 
       //{ 

       // var bytes = 
       // dataWriter.WriteBytes 
       //} 
      } 
     } 

     // the way I did it from a file that was put in the project folder, not in a PCL 
     //string path = DBPath.DBPathAsString(); 
     //bool copy = false; 
     //try 
     //{ 
     // StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(path); 
     //} 
     //catch 
     //{ 
     // copy = true; 
     //} 

     //if (copy) 
     //{ 
     // var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///" + path)); 
     // await file.CopyAsync(ApplicationData.Current.LocalFolder); 
     //} 
    } 

回答

0

我找到了答案。我不太清楚如何爲PCL做目錄,所以我把這個文件放到PCL的根目錄下。

public async Task CopyDatabase() 
    { 
     if (await CheckFileExists()) return; 

     Assembly assembly = Assembly.Load(new AssemblyName("PhenotypesLibrary")); 
     var s = assembly.GetManifestResourceStream(DBPath.FullDBPath); 

     DataReader dataReader = new DataReader(s.AsInputStream()); 
     await dataReader.LoadAsync((uint)s.Length); 
     byte[] buffer = new byte[(int)s.Length]; 
     dataReader.ReadBytes(buffer); 

     var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(DBPath.DBPathAsString); 
     await Windows.Storage.FileIO.WriteBytesAsync(file, buffer); 

    } 

    private static async Task<bool> CheckFileExists() 
    { 
     bool fileExists = false; 
     try 
     { 
      var alreadyCopiedFile = await ApplicationData.Current.LocalFolder.GetFileAsync(DBPath.DBPathAsString); 
      fileExists = true; 
     } 
     catch { } 
     return fileExists; 
    } 
    /// <summary> 
    /// The path to the database. 
    /// </summary> 
    public static string DBPathAsString = @"phenotypes_database.sqlite"; 

    /// <summary> 
    /// The Path to the file for the Assembly.GetManifestResourceStream method. 
    /// </summary> 
    public static string FullDBPath = "PhenotypesLibrary.Database.phenotypes_database.sqlite";