Windows Phone 7:文件流异常

发布于 2024-10-20 15:06:58 字数 405 浏览 8 评论 0原文

我尝试使用 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

我需要一些帮助。

I try to use FileStream (using namespace System.IO) but I get an exception :

Attempt to access the method failed 

Here is the code :

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

I searched on microsoft.com and I found that this error is because I use a bad library reference.

But in my project, I compile with mscorlib.dll from folder : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0

I need some help, please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2024-10-27 15:06:58

您将需要使用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 7isolatedStorageExplorer查看您的IsolatedStorage

。文档: http://msdn.microsoft.com/library/ff626516( v=VS.92).aspx

另请参阅:http://create。 msdn.com/en-us/education/documentation

You will need to use IsolatedStorage, for example:

Place at the top of your file:

using System.IO.IsolatedStorage;

Then in your method do this:

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");
        }
    }
}

A great example and explanation of this and other operations can be found here: http://msdn.microsoft.com/en-us/library/cc265154(v=VS.95).aspx#Y300

You can look through your IsolatedStorage by using the Windows Phone 7 IsolatedStorageExplorer

A good place to start for documentation: http://msdn.microsoft.com/library/ff626516(v=VS.92).aspx

Also here: http://create.msdn.com/en-us/education/documentation

肥爪爪 2024-10-27 15:06:58

在 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);
    }

On WindowsPhone you must use IsolatedStorage - see this tutorial for example - http://3water.wordpress.com/2010/08/07/isolated-storage-on-wp7-ii/

Read:

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

Write:

    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    using (var writeStream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
    using (var writer = new StreamWriter(writeStream))
    {
        writer.Write(content);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文