简体   繁体   中英

Read file in WP7

I must read text file in my Mobile Application on Windows Phone 7.1. I write my code :

IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader Reader = null;
try
{
Reader = new StreamReader(new IsolatedStorageFileStream("folder\\file.txt", FileMode.Open, fileStorage));
string textFile = Reader.ReadToEnd();

textBlock.Text = textFile;
Reader.Close();
}
catch
{
MessageBox.Show("File it not created");
}

All time when I try read this file, application show me MessageBox with text "file it not created". I don't have idea why application don't find my file.

Did something else create the file? If not, this is the expected behavior, as there is no file with the path. In the IsolatedStorageFileStream constructor, you passed FileMode.Open , which indicates "that the operating system should open an existing file. The ability to open the file is dependent on the value specified by the FileAccess enumeration. A System.IO.FileNotFoundException exception is thrown if the file does not exist." If you need to create the file, try FileMode.CreateNew , which indicates "that the operating system should create a new file. This requires FileIOPermissionAccess.Write permission. If the file already exists, an IOException exception is thrown" or FileMode.CreateOrOpen .

Also, you might want to consider something like the following for your catch instead. It should get you more information, making debugging faster:

catch (Exception ex)
{
    MessageBox.Show("Exception opening file: " + ex.ToString());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM