C# 列表框项保存数据

C# 列表框项保存数据,c#,.net,winforms,C#,.net,Winforms,我正在尝试制作一个视频游戏服务器管理器,我遇到了一个问题。我希望用户能够拥有尽可能多的服务器。然而,我无法通过谷歌搜索和常规的混乱来理解如何存储用户选择的与他们在列表中创建的服务器关联的信息。基本上,当您创建Server1时,它会获取您从配置屏幕上的框中选择的信息,并在服务器选择页面上使用这些信息。但是,当您创建Server2时,配置将覆盖Server1的配置。我知道我的代码甚至没有设置成能够做到这一点,但我希望能在正确的方向上推动我应该使用哪种类型的代码 Tl:dr我希望配置选项与服务器列表中

我正在尝试制作一个视频游戏服务器管理器,我遇到了一个问题。我希望用户能够拥有尽可能多的服务器。然而,我无法通过谷歌搜索和常规的混乱来理解如何存储用户选择的与他们在列表中创建的服务器关联的信息。基本上,当您创建Server1时,它会获取您从配置屏幕上的框中选择的信息,并在服务器选择页面上使用这些信息。但是,当您创建Server2时,配置将覆盖Server1的配置。我知道我的代码甚至没有设置成能够做到这一点,但我希望能在正确的方向上推动我应该使用哪种类型的代码

Tl:dr我希望配置选项与服务器列表中的ServerX关联,并且每个服务器都应该有唯一的设置

public partial class Form1 : Form
{
    //Variables
    string srvName;
    string mapSelect;
    string difSelect;


    public Form1()
    {
        InitializeComponent();
        this.srvList.SelectedIndexChanged += new System.EventHandler(this.srvList_SelectedIndexChanged);
    }

    private void srvList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(srvList.SelectedIndex == -1)
        {
            dltButton.Visible = false;
        }
        else
        {
            dltButton.Visible = true;
        }
        //Text being displayed to the left of the server listbox
        mapLabel1.Text = mapSelect;
        difLabel1.Text = difSelect;

    }
    private void crtButton_Click(object sender, EventArgs e)
    {
        //Add srvName to srvList
        srvName = namBox1.Text;
        srvList.Items.Add(srvName);

        //Selections
        mapSelect = mapBox1.Text;
        difSelect = difBox1.Text;

        //Write to config file
        string[] lines = { mapSelect, difSelect };
        System.IO.File.WriteAllLines(@"C:\Users\mlynch\Desktop\Test\Test.txt", lines);

        //Clear newPanel form
        namBox1.Text = String.Empty;
        mapBox1.SelectedIndex = -1;
        difBox1.SelectedIndex = -1;

        //Return to srvList
        newPanel.Visible = false;
    }
}

您在最近的一篇评论中提到,您曾尝试将其保存到.txt文件中,但每当您尝试创建其他文件时,它都会将其重写。如果您想继续使用.txt方法,只需设置一个全局整数变量并将其附加到保存的每个文件中即可

//Variables
string srvName;
string mapSelect;
string difSelect;
int serverNumber = 0;

...

serverNumber++;
string filepath = Path.Combine(@"C:\Users\mlynch\Desktop\Test\Test", serverNumber.ToString(), ".txt");
System.IO.File.WriteAllLines(filepath, lines);

我认为下面的源代码会给你一些关于方向的想法。让我们从一些初始化开始:

    public Form1()
    {
        InitializeComponent();
        this.srvList.SelectedIndexChanged += new System.EventHandler(this.srvList_SelectedIndexChanged);
        mapBox1.Items.Add("Germany");
        mapBox1.Items.Add("Russia");

        difBox1.Items.Add("Easy");
        difBox1.Items.Add("Difficult");
    }
这是“创建服务器”按钮的事件处理程序。它从屏幕上获取服务器参数并写入名为服务器的文件

    private void crtButton_Click(object sender, EventArgs e)
    {
        //Add srvName to srvList
        srvName = namBox1.Text;
        srvList.Items.Add(srvName);

        //Selections
        mapSelect = mapBox1.Text;
        difSelect = difBox1.Text;

        //Write to config file
        string path = @"C:\Test\" + srvName + ".txt";
        StreamWriter sw = new StreamWriter(path);
        sw.WriteLine(mapSelect);
        sw.WriteLine(difSelect);
        sw.Flush();
        sw.Close();

        //Clear newPanel form
        namBox1.Text = String.Empty;
        mapBox1.SelectedIndex = -1;
        difBox1.SelectedIndex = -1;

        //Return to srvList
        //newPanel.Visible = false;
    }
最后,下面是列表框事件处理程序。方法从文件中读取服务器参数并显示在屏幕上

    private void srvList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (srvList.SelectedIndex == -1)
        {
            dltButton.Visible = false;
        }
        else
        {
            dltButton.Visible = true;
        }

        string path = @"C:\Test\" + srvList.SelectedItem + ".txt";
        StreamReader sr = new StreamReader(path);
        //Text being displayed to the left of the server listbox
        mapLabel1.Text = sr.ReadLine();  // mapSelect;
        difLabel1.Text = sr.ReadLine();  // difSelect;

    }

请随时提出任何问题

我最终找到了问题所在。基本上,我最终决定写入一个txt文件,然后从中读取,在服务器选择菜单中显示文件的内容。我还添加了一个删除按钮,以便您可以删除您创建的服务器。它还没有完整的功能,但它已经存在了。这就是我最终得到的结果,它非常有效。谢谢大家的帮助

public partial class Form1 : Form
{
    //Variables
    string srvName;
    string mapSelect;
    string mapFile;
    string difSelect;
    string difFile;
    int maxPlayers;
    string plrSelect;
    string plrFile;
    string finalFile;
    string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    string fileName = "config.txt";


    public Form1()
    {
        InitializeComponent();
        this.srvList.SelectedIndexChanged += new System.EventHandler(this.srvList_SelectedIndexChanged);
    }
    private void srvList_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Read Server Selection
        string srvSelect = srvList.GetItemText(srvList.SelectedItem);
        string srvOut = System.IO.Path.Combine(basepath, srvSelect, fileName);
        mapFile = File.ReadLines(srvOut).Skip(1).Take(1).First();
        difFile = File.ReadLines(srvOut).Skip(2).Take(1).First();
        plrFile = File.ReadLines(srvOut).Skip(3).Take(1).First();

        //Display Server Selection
        if (srvList.SelectedIndex == -1)
        {
            dltButton.Visible = false;
        }
        else
        {
            dltButton.Visible = true;
            mapLabel1.Text = mapFile;
            difLabel1.Text = difFile;
            plrLabel1.Text = plrFile;
    }
    private void crtButton_Click(object sender, EventArgs e)
    {
        //Set Server Name
        srvName = namBox1.Text;
        string finalpath = System.IO.Path.Combine(basepath, srvName);


        //Check if server name is taken
        if (System.IO.Directory.Exists(finalpath))
        {
            MessageBox.Show("A Server by this name already exists");
        }
        else
        {
            //Add Server to the Server List
            srvList.Items.Add(srvName);

            //Server Configuration
            mapSelect = mapBox1.Text;
            difSelect = difBox1.Text;
            maxPlayers = maxBar1.Value * 2;
            plrSelect = "" + maxPlayers;

            //Clear New Server Form
            namBox1.Text = String.Empty;
            mapBox1.SelectedIndex = -1;
            difBox1.SelectedIndex = -1;

            //Create the Server File
            Directory.CreateDirectory(finalpath);
            finalFile = System.IO.Path.Combine(finalpath, fileName);
            File.Create(finalFile).Close();

            //Write to config file
            string[] lines = { srvName, mapSelect, difSelect, plrSelect };
            System.IO.File.WriteAllLines(@finalFile, lines);

            //Return to srvList
            newPanel.Visible = false;
        }
    }
}

关键在于你的问题:“如何存储信息”。您必须将设置存储在某个位置。这通常位于数据库中,但也可以位于文本文件或多个文本文件中,可能是XML或json或其他存储介质。如果我现在明白你在问什么,你的问题非常广泛,不太适合堆栈溢出。你可能首先考虑如何将数据保存在内存中。请记住,您可能需要设计一个包含每台服务器所拥有信息类型的类,然后可能需要在不同的场景中处理该类WinForm@Jonathan我实际上正在考虑这样做,但问题是当我试图让服务器写入txt文件时,我不知道如何让它为我创建的每台新服务器创建一个文件,以防止覆盖上一台服务器。我的下一个问题是美观,但我希望当您从列表中选择服务器时,服务器的特定属性可以查看。我不想显示所有的信息,但是没有办法让C从txt文件中读取特定的行,至少我没有发现。我很高兴你提出了一种有效的方法。您可能希望将信息以某种结构化格式存储在文本文件中,如json或xml。然后在读写时对其进行序列化/反序列化。这样,在代码中就可以使用强类型对象。问题是,将信息写入文本文件的方式是一种结构化格式。该文件的唯一用途是存储稍后将显示给用户的信息,而文件ReadLines正是允许我获取所需特定行的内容。