如何在 Winforms 中保存用户名?

How to save an username in Winforms?

我正在尝试保存在“输入玩家名”字段中输入的用户名以创建高分列表,有人可以帮助我吗?

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();
        }
    }
}

基本上你有两个选择,你可以将信息保存到文件,或者将它保存到数据库。这应该可以帮助您:Best way to store data locally in .NET (C#)

在提问之前总是尝试自己搜索!

你可以在你的项目文件夹中写入txt文件,重启应用程序后,你可以控制该位置的文件,如果它存在你可以读取数据并放置表格。 像一个饼干。 示例代码。

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.        
                           
          }
     }
}

windows-forms 应用程序最推荐的选项是位于 Properties 文件夹中的设置文件。 那里的数据可以按用户和按应用程序保存,并存储在应用程序的 AppData 文件夹中。
您可以阅读设置-table 字段,更改它们的值并保存您的更改。

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

阅读 this 问题了解更多信息。