Home > Mobile >  How to save an username in Winforms? [closed]
How to save an username in Winforms? [closed]

Time:09-17

I am trying to save the username entered in the "enter playername" field in order to create a highscore list, can anyone help me with that ?

namespace TikTakTo
{
    public partial class Anmeldung : Form
    {
        public Anmeldung()
        {
            InitializeComponent();
        }

        private void button_play_Click(object sender, EventArgs e)
        {
            Form1.setSpielerName(Spieler_1.Text, Spieler_2.Text);

            Form1 frm = new Form1();
            frm.Show();

            this.Hide();
        }

        private void Spieler_2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == "\r")
                button_play.PerformClick();
        }
    }
}

CodePudding user response:

Basically you have two options, you can either save the info to file, or save it to database. This should help you: Best way to store data locally in .NET (C#)

Always try to make your own search before asking a question!

CodePudding user response:

you can write in txt file in your project folder , and after restarting the app, you can control the file in that location , if its exist you can read data and put the form. like a cookie. example code.

public static string _debugPath { get { return Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); } }
        
public static void WriteToText(string txt)
{
  DateTime _dt = DateTime.Now;
  using (StreamWriter wr = File.AppendText(Path.Combine(_debugPath, "Info.Inc")))
  {
    wr.WriteLine(txt "|");
    wr.Close();
  }
}
public static bool ReadInfo()
{
    FileInfo fi = new FileInfo(Path.Combine(_debugPath , "Info.Inc"));
    if (fi.Exists)
    {
          using (StreamReader rd = new StreamReader(Path.Combine(_debugPath , "Info.Inc")))
          {
                          
                  //take data in here and put the form.        
                           
          }
     }
}

CodePudding user response:

Most recommanded option for windows-forms applications is a settings file located in the Properties folder. The data there can be saved per-user and per-application, and stored in the application's AppData folder.
You can read the settings-table fields, change their values and save your changes.

e.g:
Properties.Settings.Default["SomeProperty"] = "Some Value";
Properties.Settings.Default.Save();

Read this question for more info.

  • Related