我已經創造了一些SQLite數據庫,並把它在我的項目內容,文件在這個地方複製從「源代碼目錄」獨立存儲
我怎樣才能第一時間檢查用戶安裝應用程序並將這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;
}
}
只需檢查數據庫文件在複製之前是否在隔離存儲中退出 – bit 2014-10-02 04:51:19