Windows Phone 7隔离存储-不允许操作 [英] windows phone 7 isolated storage - operation not permitted

查看:92
本文介绍了Windows Phone 7隔离存储-不允许操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在研究一个简单的游戏,我想实现一个高分系统.播放器首次加载主页后,便会创建一个新的文本文件("hsc.txt"),并插入了一些假值,这些假值随后会被程序拆分,但是,目前,我的代码抛出而且我似乎找不到问题.我查找了从消息框"- operation not permitted"中收到的错误,但发布的所有解决方案似乎均不起作用.我尝试关闭流,但似乎不起作用.

So I've been working on a simple game and I wanted to implement a highscore system. Once the player loads up the main page for the first time a new text file is created ("hsc.txt") and some fake values are inserted which are later on split up by the program, however, currently my code throws a System.IO.IsolatedStorage.IsolatedStorageException and I can't seem to find the problem. I've looked up the error that I got from the message box which was "- operation not permitted" but all the solutions that were posted don't seem to work. I have tried closing the streams but it doesn't seem to work.

任何建议将不胜感激.

 private void hasHighscores()
      {
        String fileName = "hsc.txt";
        using  (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!isoStorage.FileExists(fileName))
            {
                isoStorage.CreateFile(fileName);

                       using (var isoStream = new IsolatedStorageFileStream(fileName,   FileMode.Append, FileAccess.Write, isoStorage))
                    {
                        using (var fileStream = new StreamWriter(isoStream))
                        {
                            fileStream.WriteLine("n1:666,n2:777,n3:888,h1:666,h2:777,h3:888");
                            fileStream.Close();
                        }
                        isoStream.Close();

                    }
            }
        }  
    }

到目前为止,我已经:a)更改了FileMode b)更改了FileAccess和我什至不记得的其他一些快速修正".

So far I have: a) changed the FileMode b) changed the FileAccess and a few other "quickfixes" that I don't even remember.

推荐答案

CreateFile方法将流返回到创建的文件,并保持打开状态.因此,当您尝试在下一行中打开到同一文件的流时,由于该文件已被锁定,它将引发异常.

The CreateFile method returns a stream to the created file, and keeps it open. Therefore, when you try to open a stream to that same file in the next line, it throws an exception because the file is already locked.

您可以按以下方式重写代码:

You can rewrite your code as follows:

 private void hasHighscores()
 {
        String fileName = "hsc.txt";
        using  (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!isoStorage.FileExists(fileName))
            {
                using (var isoStream = isoStorage.CreateFile(fileName))
                {
                    using (var fileStream = new StreamWriter(isoStream))
                    {
                        fileStream.WriteLine("n1:666,n2:777,n3:888,h1:666,h2:777,h3:888");
                    }  
                }
            }
        }  
    }

我还删除了stream.Close()指令.将流包含在using语句中时,将自动调用close方法.

I've also removed the stream.Close() instructions. The close method is automatically called when you enclose the stream in a using statement.

这篇关于Windows Phone 7隔离存储-不允许操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆