IsolatedStorageFileStream上不允许操作

时间:2012-11-05 12:03:31

标签: windows-phone-7 isolatedstorage

    using (EntityDataContext amdb = new EntityDataContext(StrConnectionString))
            {
                if (amdb.DatabaseExists())
                {
                    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!isoStore.FileExists(databaseName))
                        {
                            copyDatabase = true;
                        }
                        else
                        {
                            using (IsolatedStorageFileStream databaseStream = isoStore.OpenFile(databaseName, FileMode.Open, FileAccess.Read)) // error here
                            {
                                using (Stream db = Application.GetResourceStream(new Uri(databaseName, UriKind.Relative)).Stream)
                                {
                                    if (databaseStream.Length < db.Length)
                                        copyDatabase = true;
                                }
                            }
                        }
                    }
                }
                else
                {
                    //error with the database that has been packaged
                }
                if (copyDatabase)
                    new Worker().Copy(databaseName);
            }

2 个答案:

答案 0 :(得分:0)

在隔离存储访问模式参数中检查您是否精确,可以在那里写入数据,而不仅仅是能够读取。

您是否使用设备进行测试?

答案 1 :(得分:0)

我可以告诉您正在尝试读取数据库文件,而您仍然打开了数据库连接。由于DataContext锁定数据库(以及文件),因此不允许同时读取它。

要关闭数据库连接,请尝试关闭EntityDataContext对象(通过调用amdb.Close()或关闭using语句

尝试这样的事情:

bool shouldCopyDatabase = false;
bool databaseExists = false;

using (EntityDataContext amdb = new EntityDataContext(StrConnectionString))
{
    databaseExists = amdb.DatabaseExists();
}

if (databaseExists == true)
{
    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!isoStore.FileExists(databaseName))
        {
            copyDatabase = true;
        }
        else
        {
            using (IsolatedStorageFileStream databaseStream = isoStore.OpenFile(databaseName, FileMode.Open, FileAccess.Read)) // error here
            {
                using (Stream db = Application.GetResourceStream(new Uri(databaseName, UriKind.Relative)).Stream)
                {
                    if (databaseStream.Length < db.Length)
                        copyDatabase = true;
                }
            }
        }
    }
}

if (copyDatabase)
    new Worker().Copy(databaseName);

通过将隔离存储访问功能移到using (EntityDataContext amdb = new EntityDataContext(StrConnectionString))范围之外,您可以先关闭数据库连接。

相关问题