Reputation: 51
My objective is to save "textbox" input to a text file and then being able to load that saved text from the same text file back to a textbox.
I think that one of my mistakes is reading on Console.
namespace aaa
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string fileName = "test.txt";
FileStream fs = null;
fs = new FileStream(fileName, FileMode.CreateNew);
StreamWriter writer = new StreamWriter(fs);
writer.Write(textBox1.Text);
}
private void btnWrite_Click(object sender, RoutedEventArgs e)
{
}
private void btnRead_Click(object sender, RoutedEventArgs e)
{
public static void Main()
{
using (StreamReader sr = new StreamReader("test.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
}
}
Upvotes: 4
Views: 4359
Reputation: 9417
in windows phone you don't have such a direct access to the file system.
you can use the isolated file storage to read and write files.
here you find a broad overview of the features:
https://msdn.microsoft.com/library/system.io.isolatedstorage.isolatedstoragefile
(Caution: This is for windows phone 7, for windows phone 8.1 see the other answer)
Upvotes: 0
Reputation: 21919
Use Windows.Storage.StorageFile in both Windows Phone Runtime and Windows Phone Silverlight apps.
There are examples in the MSDN documentation in the Working with data and files section. In particular see Quickstart: Local app data or Quickstart: Roaming app data and Quickstart: Reading and writing files
This last Quickstart has demos directly on reading and writing short text snippets to a file:
// Create sample file; replace if exists.
StorageFolder folder =
Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile =
await folder.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteTextAsync(sampleFile, textBox1.text);
Upvotes: 6