Sankar Guda
Sankar Guda

Reputation: 11

In C# on windows phone Attempt to access the method failed: System.IO.FileStream..ctor(System.String, System.IO.FileMode)

FileStream FS = new FileStream("MyFolder\\MyFile.txt", FileMode.Open);
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\MyFile.txt", FileMode.Append, myIsolatedStorage));

    using (writeFile)
    {
        FS.Seek(0, SeekOrigin.End);
        writeFile.WriteLine(txtWrite.Text);
        writeFile.Close();
        System.Diagnostics.Debug.WriteLine("Now I am here");
    }

When I am trying to run this code(trying to append data into an existing text file), getting exception

"Attempt to access the method failed: System.IO.FileStream..ctor(System.String, System.IO.FileMode)"

What is the mistake I have done here?

Upvotes: 0

Views: 2648

Answers (3)

Sankar Guda
Sankar Guda

Reputation: 11

Finally I made it working after struggling for 4 hrs:

 IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();                
            StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\MyFile.txt", FileMode.Append, myIsolatedStorage));

            writeFile.Flush();

            System.Diagnostics.Debug.WriteLine(txtWrite.Text);
            writeFile.WriteLine(txtWrite.Text);                
            writeFile.Close();
            System.Diagnostics.Debug.WriteLine("Now I am here");

I removed the file stream method and did some modifications. Its started to work. Thanks to everybody who tried to help me with your suggestions

Upvotes: 0

Richard Szalay
Richard Szalay

Reputation: 84734

Don't use the FileStream class directory. Get your streams via the methods on IsolatedStorageFile:

IsolatedStorageFile myIsolatedStorage = 
    IsolatedStorageFile.GetUserStoreForApplication();

using (var writeFile = myIsolatedStorage.OpenFile("MyFolder\\MyFile.txt", FileMode.Append))
using (var writeFileStream = new StreamWriter(writeFile))
{
    writeFileStream.WriteLine(txtWrite.Text);
    System.Diagnostics.Debug.WriteLine("Now I am here");
}

Upvotes: 2

Matt Lacey
Matt Lacey

Reputation: 65564

Could it be that you're attempting to open the same file twice?

A version of your question (with an answer) can be seen at How to append data into the same file in IsolatedStorage for Windows Phone

Upvotes: 0

Related Questions