简体   繁体   中英

How to read textfile line by line in WP7 APP?

I'm writing a very simple WP7 app. The it will by kind of notepad. My task is to create an account for user- I'm doing it by saving data in textfile. I don't have any idea how to read logins and passwords from the text file. Of course the best solution will by database but I don't have enough skills to do that.

Here is my code.

Reading and saving:

        private void addUserToFile()
    {
        string fileName = "Logins.txt";

        using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
            {
                // opens the file and writes to it.
                using (var fileStream = new StreamWriter(isoStream))
                {
                    fileStream.WriteLine(typeLogin.Text+"\t"+typePassword.Password);

                }
            }

            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }

And finally reading from textfile ( no idea how to iterate line by line, logins and passwords are separated by TAB):

private void bGoToPage2_Click(object sender, RoutedEventArgs e) //przechodzenie na druga strone
        {


        if (string.Equals(passwordBox1.Password,passwordBox2.Password))
        {
            MessageBox.Show("Passwords match!");


            if (Login1.Text == "admin") //how change it to reading from file????
            {

                NavigationService.Navigate(new Uri("/Passwords.xaml?param=" + Login1.Text, UriKind.RelativeOrAbsolute));
            }
            else
            {
                MessageBox.Show("Nie znaleziono użytkownika");
            }
        }
        else
        {
            MessageBox.Show("Passwords do not match!");
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        }

If you want to read, simply replace the writer class with reader class.

// opens the file and writes to it.
using (var fileStream = new StreamReader(isoStream))
{
    var login = fileStream.ReadLine();
    var password = fileStream.ReadLine();
}

You can now write login and password into its own line.

Hope this helps. Although I wouldn't recommend saving user name and password like that in clear text.

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