Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不允许对IsolatedStorageFileStream执行操作错误_C#_Silverlight_Windows Phone 7 - Fatal编程技术网

C# 不允许对IsolatedStorageFileStream执行操作错误

C# 不允许对IsolatedStorageFileStream执行操作错误,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,当我试图将文件内容保存在fileStream fs中时,它给出了在IsolatedStorageFileStream上不允许的操作错误 var appStorage = IsolatedStorageFile.GetUserStoreForApplication(); string[] fileList = appStorage.GetFileNames(); foreach (string fileName in fileList) { using (var file

当我试图将文件内容保存在fileStream fs中时,它给出了在IsolatedStorageFileStream上不允许的
操作错误

var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileList = appStorage.GetFileNames();

foreach (string fileName in fileList)
    {
       using (var file = appStorage.OpenFile(fileName, FileMode.Open))
       {
           if (fileName != "__ApplicationSettings")
           {
               var fs = new IsolatedStorageFileStream(fileName, FileMode.Open, FileAccess.Read, appStorage);
               string abc = fs.ToString();
               meTextBlock.Text = abc;
               //MemoryStream ms = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read);

               clientUpload.UploadAsync(SkyDriveFolderId, fileName, fs);
          }
     }
}

为什么要使用(var file=appStorage.OpenFile(fileName,FileMode.Open))添加内部

在我看来,问题在于您正在打开一个流来读取文件,然后打开另一个流,而没有关闭上一个流

如果你删除了这条线(似乎没有做任何事情),它应该可以正常工作


哦,
fs.ToString()
只会得到类型名,而不是文件内容;要读取文件,请将
StreamReader
fs

一起使用。当一个流(或读卡器或其他)打开一个孤立的存储文件,并且在第一个流(或读卡器或其他)尚未放弃该文件时,另一个对象正在访问该文件时,此错误始终发生。在访问隔离存储文件的所有位置仔细检查代码,确保在其他文件访问它之前关闭每个文件。佩德罗·拉马斯对于这个特殊的案例是正确的,我只是想提供一些一般性的反馈。如果你在google上搜索“IsolatedStorageFileStream错误上不允许操作”的问题和答案,你会看到趋势。不过,错误消息可能更具描述性。

试试这种方法

using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (IsolatedStorageFile.IsEnabled)
                    {
                        if (isf.FileExists(localFileName))
                        {
                            using (var isfs = new IsolatedStorageFileStream(localFileName, FileMode.Open, isf))
                            {
                                using (var sr = new StreamReader(isfs))
                                {
                                    var data = sr.ReadToEnd();
                                    if (data != null)
                                    {
                                       ...

天啊!非常感谢佩德罗喇嘛。问题在于
使用(var file=appStorage.OpenFile(fileName,FileMode.Open))
谢谢!我有确切的问题,你解决了!在开始使用另一个文件流之前,我必须关闭该文件流