2014-10-02 40 views
0

我已經創造了一些SQLite數據庫,並把它在我的項目內容,文件在這個地方複製從「源代碼目錄」獨立存儲

enter image description here

我怎樣才能第一時間檢查用戶安裝應用程序並將這2個數據庫複製到獨立存儲?


我已經試過這種方式,但它給了我一個錯誤"Object reference not set to an instance of an object."在行

using (Stream stream = Application.GetResourceStream(new Uri("/Assets/DBS/MyPlaylist.db", UriKind.Relative)).Stream) 

這裏是我的代碼

private static string DataFolder = "Datas";  
public static void FirstTimeCopyDB() 
    { 
     using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      if (!isf.DirectoryExists(DataFolder)) 
      { 
       isf.CreateDirectory(DataFolder); 
       using (Stream stream = Application.GetResourceStream(new Uri("/Assets/DBS/MyPlaylist.db", UriKind.Relative)).Stream) 
       { 
        using (IsolatedStorageFileStream isfs = isf.CreateFile("Datas/MyPlaylist.db")) 
        { 
         byte[] buffer = new byte[1024]; 
         int byteRead = -1; 
         while ((byteRead = stream.Read(buffer, 0, buffer.Length)) > 0) 
         { 
          isfs.Write(buffer, 0, byteRead); 
         } 
        } 
       } 
       using (Stream stream = Application.GetResourceStream(new Uri("/Assets/DBS/MyDatabase.db",UriKind.Relative)).Stream) 
       { 
        using (IsolatedStorageFileStream isfs = isf.CreateFile("Datas/MyDatabase.db")) 
        { 
         byte[] buffer = new byte[1024]; 
         int byteRead = -1; 
         while ((byteRead = stream.Read(buffer,0,buffer.Length))>0) 
         { 
          isfs.Write(buffer,0,byteRead); 
         } 
        } 
       } 
      } 
      else return; 
     } 
    } 
+1

只需檢查數據庫文件在複製之前是否在隔離存儲中退出 – bit 2014-10-02 04:51:19

回答

2

事情是這樣的:

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    if (!store.FileExists("yourDBfileName")) 
    { 
    // copy yourDBfile 
    } 
} 

探索更多here

+0

我已經添加了一些信息,希望您能幫助我:D – user3448806 2014-10-02 05:12:15

+1

您的路徑錯誤。 – bit 2014-10-02 05:32:59

+0

好吧,我發現我必須添加一些標題到路徑,現在它工作:)'使用(Stream stream = Application.GetResourceStream(new Uri(「phone_mp3; component/Assets/DBS/MyPlaylist.db」,UriKind ;相對))。流)' – user3448806 2014-10-02 14:59:20