I'm currently working on a project that requires me to store usernames and passwords into a text file, what i've got so far is that i'm able to write to the text file by using this

//string path = "C:\\Documents and Settings\\100000001791\\My Documents\\Visual Studio 2008\\Projects\\SmartDeviceProject1\\SmartDeviceProject1\\bin\\Debug\\usernames.txt";
            string path = "\\Temp\\users.txt";
            string line = Username.Text + "," + Password.Text ;

            if (!File.Exists(path))
            {
                MessageBox.Show(Username.Text + " successfully created.");
                FileStream cred = new FileStream(path, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(cred);
                sw.WriteLine(line);
                sw.Close();
                cred.Close();
            }
            else
            {
                MessageBox.Show(Username.Text + " successfully created");
                FileStream creds = new FileStream(path, FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(creds);
                sw.WriteLine(line);
                sw.Close();
                creds.Close();
            }

however, i have no idea how to go about using the data that i've created, what i've tried so far, is this.

private void LoginBtn_Click(object sender, EventArgs e)
        {
            string path = "C:\\Documents and Settings\\100000001791\\My Documents\\Visual Studio 2008\\Projects\\SmartDeviceProject1\\SmartDeviceProject1\\bin\\Debug\\usernames.txt";
            //string path = "\\Temp\\loginlog.txt";
            string line = Username.Text + "," + Password.Text + "," + DateTime.Now;

            if (!File.Exists(path))
            {
                //FileStream users = new FileStream("\\Temp\\users.txt", FileMode.Open, FileAccess.Read);
                FileStream users = new FileStream("C:\\Documents and Settings\\100000001791\\My Documents\\Visual Studio 2008\\Projects\\SmartDeviceProject1\\SmartDeviceProject1\\bin\\Debug\\users.txt", FileMode.Open, FileAccess.Read);
                StreamReader su = new StreamReader(users);

                if (line == su.ReadLine())
                {
                    MessageBox.Show(Username.Text + " successfully logged in");
                    FileStream cred = new FileStream(path, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(cred);
                    sw.WriteLine(line);
                    sw.Close();
                    cred.Close();

                    AdminPage f2 = new AdminPage();
                    f2.Show();
                }
                else
                {
                    MessageBox.Show("Incorrect Username/Password");
                }
            }

            else
            {
                //FileStream users = new FileStream("\\Temp\\users.txt", FileMode.Open, FileAccess.Read);
                FileStream users = new FileStream("C:\\Documents and Settings\\100000001791\\My Documents\\Visual Studio 2008\\Projects\\SmartDeviceProject1\\SmartDeviceProject1\\bin\\Debug\\users.txt", FileMode.Open, FileAccess.Read);
                StreamReader su = new StreamReader(users);

                if (line == su.ReadLine())
                {
                    MessageBox.Show(Username.Text + " successfully logged in");
                    FileStream creds = new FileStream(path, FileMode.Append, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(creds);
                    sw.WriteLine(line);
                    sw.Close();
                    creds.Close();

                    AdminPage f4 = new AdminPage();
                    f4.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Incorrect Username/Password");
                }
            }

Any help is deeply appreciated, and thank you so, so, so much.
I'm literally pulling my hair out over this :(

Recommended Answers

All 3 Replies

and the code i've tried is obviously nonsense and not working. :(

I would suggest to use BinaryReader/Writer or serialization. Have a look at this code:

public class Login
{
    public string UserName { get; set; }
    public string Password { get; set; }

    static string path = @"c:\folder\p1.txt";
    public static void WriteUser(Login obj)
    {
        BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.Create));
        bw.Write(obj.UserName);
        bw.Write(obj.Password);
        bw.Close();
    }
    public static void ReadUser(Login obj)
    {
        BinaryReader br = new BinaryReader (File.Open(path, FileMode.Open));
        obj.UserName = br.ReadString();
        obj.Password = br.ReadString();
        br.Close();
    }
}

class Test
{
    static void Main()
    {
        Login a = new Login();
        
        a.UserName = "user1";
        a.Password = "pass1";
        Login.WriteUser(a);

        Login.ReadUser(a);
        Console.WriteLine(a.UserName + " " + a.Password);
    }
}

I'll give it a try, thanks alot :D!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.