Friday, September 26, 2014

Easy text files on Windows Phone?

Perhaps unintuitively, the first thing I'm going to do is make a quick Windows Phone app, just to show how easy it is to target "all three" mobile platforms with C#. But first, a digression into text files. These are pretty easy to handle in Xamarin, but if you look up how to save and load to and from files on Windows Phone, you usually get some pretty convoluted results utilizing async files and localized storage madness.

I don't think you had as much of System.IO in Windows Phone 7, but 8 seems to have pretty much all of the standard stuff you expect on a desktop running .NET.

So now that I've got a Windows Phone handy (an HTC 8XT) running Windows Phone 8, I tried to make an insanely basic app to try reading and writing files. Using really standard using (StreamWriter sw = File.CreateText(this.strFileLoc)) seems to work without issue. I've tried running the app, entering & then saving some text, turning the phone off & back on, and the file's contents are still there.

Here's what you do... Start an app. Throw on two buttons, call 'em btnSave and btnLoad. Throw in a single textbox, txtText. Make sure you click on AcceptsReturn for the textbox, just to keep yourself sane while you're typing.



And then here's MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TextToFile.Resources;
using System.IO;

namespace TextToFile
{
    public partial class MainPage : PhoneApplicationPage
    {
        public string strFileLoc = "";
        public MainPage()
        {
            InitializeComponent();

            Windows.ApplicationModel.Package package =
                Windows.ApplicationModel.Package.Current;
            Windows.Storage.StorageFolder installedLocation = 
                package.InstalledLocation;
            this.strFileLoc = Path.Combine(installedLocation.Path, 
                "myFile.txt");
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string strToWrite = this.txtText.Text;
            using (StreamWriter sw = File.CreateText(this.strFileLoc))
            {
                sw.WriteLine(strToWrite);
                sw.Close();
            }
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            string strText = string.Empty;
            if (File.Exists(this.strFileLoc))
            {
                using (StreamReader sr = 
                    new StreamReader(File.OpenRead(this.strFileLoc)))
                {
                    strText = sr.ReadToEnd();
                }
            }
            else
            {
                strText = "File doesn't exist";
            }
            this.txtText.Text = strText;
        }
    }
}
So far so good. What am I missing?

No comments:

Post a Comment