【问题标题】:Windows Phone 7 : FileStream exceptionWindows Phone 7:文件流异常
【发布时间】:2011-03-08 14:48:30
【问题描述】:

我尝试使用 FileStream(使用命名空间 System.IO),但出现异常:

Attempt to access the method failed 

代码如下:

FileStream fs = new FileStream("file.txt", FileMode.Create); 

我在microsoft.com上搜索,发现这个错误是因为我使用了错误的库引用。

但在我的项目中,我使用文件夹中的 mscorlib.dll 进行编译:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0

我需要一些帮助,拜托。

【问题讨论】:

    标签: exception windows-phone-7 filestream


    【解决方案1】:

    您需要使用IsolatedStorage,例如:

    放在文件顶部:

    using System.IO.IsolatedStorage;
    

    然后在你的方法中这样做:

    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var istream = new IsolatedStorageFileStream("File.txt", FileMode.OpenOrCreate, store))
        {
            using (var sw = new StreamWriter(istream))
            {
                sw.Write("Some Stuff");
            }
        }
    }
    

    这个和其他操作的一个很好的例子和解释可以在这里找到:http://msdn.microsoft.com/en-us/library/cc265154(v=VS.95).aspx#Y300

    您可以使用Windows Phone 7 IsolatedStorageExplorer查看您的独立存储

    一个很好的文档起点:http://msdn.microsoft.com/library/ff626516(v=VS.92).aspx

    也在这里:http://create.msdn.com/en-us/education/documentation

    【讨论】:

    • 谢谢,它有效。你有一个链接,我可以找到适用于 windows phone sdk 的所有功能吗?因为,当我尝试使用 .NET 4 功能时,有时它在 windows phone 7 上不可用
    • 另一个问题,是否可以从我的计算机查看存储中创建的文件?
    • @TheFrancisOne 我在答案中添加了应该有帮助的链接
    【解决方案2】:

    在 WindowsPhone 上,您必须使用 IsolatedStorage - 例如,请参阅本教程 - http://3water.wordpress.com/2010/08/07/isolated-storage-on-wp7-ii/

    阅读:

        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        using (var readStream = new IsolatedStorageFileStream(fileName, FileMode.Open, store))
        using (var reader = new StreamReader(readStream))
        {
            return reader.ReadToEnd();
        }
    

    写:

        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        using (var writeStream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
        using (var writer = new StreamWriter(writeStream))
        {
            writer.Write(content);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多