问题
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?
回答1:
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");
}
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/10719100/in-c-sharp-on-windows-phone-attempt-to-access-the-method-failed-system-io-files