C#如何将存储的文件限制在100项?

C#如何将存储的文件限制在100项?,c#,C#,我试图制作一个小程序,接收用户输入并将其存储在文件中,但我希望该文件最多包含100个元素: 假设用户添加了100个名称,用户添加的下一个名称将显示一条消息“列表已满” 以下是我迄今为止完成的代码: public Form1() { InitializeComponent(); } private string SongName, ArtistName; public void Registry() { List&l

我试图制作一个小程序,接收用户输入并将其存储在文件中,但我希望该文件最多包含100个元素:

假设用户添加了100个名称,用户添加的下一个名称将显示一条消息“列表已满”

以下是我迄今为止完成的代码:

    public Form1()
    {
        InitializeComponent();
    }
    private string SongName, ArtistName;

    public void Registry()
    {
        List<Name> MusicList = new List<Name>(); //Create a List
        MusicList.Add(new Name(SongName = txtSongName.Text, ArtistName = txtArtistName.Text)); //Add new elements to the NameClass

        //check if the input is correct
        if (txtSongName.TextLength < 1 || txtArtistName.TextLength < 1)
        {
            Info info = new Info();
            info.Show();
        }
        else //if input is correct data will be stored
        { 
            //Create a file to store data
            StreamWriter FileSaving = new StreamWriter("MusicList", true);
            for (int i = 0; i < MusicList.Count; i++)
            {
                string sName = MusicList[i].songName; //Create new variable to hold the name
                string aName = MusicList[i].artistName; //Create new variable to hold the name
                FileSaving.Write(sName + " by "); //Add SongName to the save file
                FileSaving.WriteLine(aName); //Add ArtistName to the save file

            }
            FileSaving.Close();
        }
    }

    private void btnEnter_Click(object sender, EventArgs e)
    {
        Registry();
        //Set the textbox to empty so the user can enter new data
        txtArtistName.Text = "";
        txtSongName.Text = "";
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
public Form1()
{
初始化组件();
}
私有字符串SongName、ArtistName;
公共无效登记处()
{
List MusicList=new List();//创建一个列表
MusicList.Add(新名称(SongName=txtSongName.Text,ArtistName=txtArtistName.Text));//向NameClass添加新元素
//检查输入是否正确
if(txtSongName.TextLength<1 | | txtaristname.TextLength<1)
{
信息=新信息();
info.Show();
}
否则//如果输入正确,将存储数据
{ 
//创建一个文件来存储数据
StreamWriter FileSaving=新的StreamWriter(“MusicList”,true);
for(int i=0;i
private const int MAX\u storaged\u SONGS=100//as类级别字段
对于(int i=0;i最大存储歌曲数)
errorMessageLabel.Text=“列表已满,仅添加了100项”
我不确定你的列表选择器是什么样子的,但是你可能想通过在提交页面之前使用一些javascript/验证客户端来阻止他们选择超过100个项目

您的代码不清楚的是,当用户提交一首歌曲时,您创建了一个新的空音乐列表,向其中添加了一个项目,但您在其中循环,就好像有多个项目一样。也许您应该先读取文件以确定其中有多少首歌曲,这样您就可以确定它何时为100首歌曲。

private const int MAX_STORED_songs=100//as类级别字段
对于(int i=0;i最大存储歌曲数)
errorMessageLabel.Text=“列表已满,仅添加了100项”
我不确定你的列表选择器是什么样子的,但是你可能想通过在提交页面之前使用一些javascript/验证客户端来阻止他们选择超过100个项目


您的代码不清楚的是,当用户提交一首歌曲时,您创建了一个新的空音乐列表,向其中添加了一个项目,但您在其中循环,就好像有多个项目一样。也许您应该先读取文件以确定其中有多少首歌曲,这样您就可以确定何时有100首歌曲。

您可能希望尝试使用xml为数据提供某种结构

如果你想保持当前格式,你唯一的选择就是计算文件中的新行数,看看这个数字加上音乐列表中的任何新项目是否会超出你的限制

List<string> lines = new List<string>(System.IO.File.ReadAllLines(MyFile));
lines.Add(sName + " by " + aName);

int lineCount = lines.Count;
//limit reached
if(lineCount > 100 )
{
    //TODO: overlimit code
} else {
    System.IO.File.WriteAllLines(MyFile, lines.ToArray());
}
List lines=新列表(System.IO.File.ReadAllLines(MyFile));
行。添加(sName+“by”+aName);
int lineCount=行数;
//达到极限
如果(行数>100)
{
//TODO:超限代码
}否则{
System.IO.File.writeAllines(MyFile,lines.ToArray());
}

您可能希望尝试使用xml为数据提供某种结构

如果你想保持当前格式,你唯一的选择就是计算文件中的新行数,看看这个数字加上音乐列表中的任何新项目是否会超出你的限制

List<string> lines = new List<string>(System.IO.File.ReadAllLines(MyFile));
lines.Add(sName + " by " + aName);

int lineCount = lines.Count;
//limit reached
if(lineCount > 100 )
{
    //TODO: overlimit code
} else {
    System.IO.File.WriteAllLines(MyFile, lines.ToArray());
}
List lines=新列表(System.IO.File.ReadAllLines(MyFile));
行。添加(sName+“by”+aName);
int lineCount=行数;
//达到极限
如果(行数>100)
{
//TODO:超限代码
}否则{
System.IO.File.writeAllines(MyFile,lines.ToArray());
}

你陈述了你想要实现的目标和到目前为止已经完成的工作,但是忘记提到你面临的问题是什么,为什么你不能前进。当你说
显示消息“列表已满”
时,你是指一个消息框吗?你陈述了你想要实现的目标和到目前为止已经完成的工作,但是忘了提到你面临的问题是什么,为什么你不能前进。当你说
显示消息“列表已满”
时,你指的是消息框吗?继续,慷慨一些。夹头在a
const int MAX_storaged_SONGS=100@MatthewWatson遵守惯例,加油@安东尼金:我同意。研究还表明,所有大写字母都很难阅读,因为我们看到的单词/字母中只有不到10%是大写的。然而,作为程序员,我尊敬的同事们对常量使用全上限约定,我总是避免这场不值得一战的战斗。好吧,他们确实倾向于脱颖而出——他们应该非常罕见。继续,慷慨一些。夹头在a
const int MAX_storaged_SONGS=100@MatthewWatson遵守惯例,加油@安东尼金:我同意。研究还表明,所有大写字母都很难阅读,因为我们看到的单词/字母中只有不到10%是大写的。然而,作为程序员,我尊敬的同事们对常量使用全上限约定,我总是避免这场不值得一战的战斗