我已经创建了一些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;
}
}
答案 0 :(得分:2)
这样的事情:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.FileExists("yourDBfileName"))
{
// copy yourDBfile
}
}
了解更多here