-1

我目前正在尝试制作一个小程序,将用户在文本框中的输入存储到文件中,但不知何故我无法将输入保存到文件中,这就是我到目前为止所做的:

头等舱:

class Name
{
    private string SongName;
    private string ArtistName;

    //Constructor
    public Name(string SongName, string ArtistName)
    {
        this.ArtistName = ArtistName;
        this.SongName = SongName;
    }

    //Properties for SongName
    public string songName
    {
        get { return SongName; }
        set { SongName = value; }
    }

    //Properties for ArtistName
    public string artistName
    {
        get { return ArtistName; }
        set { ArtistName = value; }
    }
}

这是 Form1 代码:

    public Form1()
    {
        InitializeComponent();
    }

    private string SongName, ArtistName;

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

        //Save the list
        StreamWriter FileSaving = new StreamWriter("MusicList");
        for (int i = 0; i < MusicList.Count; i++)
        {
            string sName = MusicList[i].songName;
            string aName = MusicList[i].artistName;
        }
        FileSaving.Close();
    }

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

5 回答 5

2

....您从不向文件写入任何内容.....

一个快速的谷歌给了我这个

请注意对...的调用file.WriteLine(line);...您意识到您也没有创建带有扩展名的文件吗?

于 2013-03-20T19:20:38.100 回答
2

在您的 for 循环中,您需要将值实际写入流。

for (int i = 0; i < MusicList.Count i++)
{
    string sName = MusicList[i].songName;
    string aName = MusicList[i].artistName;
    FileSaving.WriteLine(sName + ' ' + aName);
}
于 2013-03-20T19:21:07.097 回答
1

在这里,您正在创建StreamWriter但从不使用它:

StreamWriter FileSaving = new StreamWriter("MusicList");
for (int i = 0; i < MusicList.Count; i++)
{
    string sName = MusicList[i].songName;
    string aName = MusicList[i].artistName;
}
FileSaving.Close();

字符串 aName 和 sName 已分配给但从未保存。

请参阅此处有关使用 StreamWriter 的示例:

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx

于 2013-03-20T19:20:38.050 回答
1
using (StreamWriter writer = new StreamWriter(filePath))
{
        for (int i = 0; i < MusicList.Count; i++)
        {
            writer.WriteLine(MusicList[i].songName + " , " + MusicList[i].artistName);
        }
}
于 2013-03-20T19:21:23.813 回答
0

我认为你应该使用 XML 序列化

这是我的序列化泛型类

public static class Serializer<T>
{
    public static T Deserialize(string path)
    {
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

        System.IO.FileStream stream = null;

        try
        {
            stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

            return (T)serializer.Deserialize(stream);
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
                stream.Dispose();
            }
        }
    }

    public static void Serialize(string path, T obj, bool createFolder = false)
    {
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

        if (createFolder)
        {
            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
        }

        using (System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(path, System.Text.Encoding.Unicode))
        {
            xmlWriter.Formatting = System.Xml.Formatting.Indented;

            serializer.Serialize(xmlWriter, obj);

            xmlWriter.Flush();
            xmlWriter.Close();
        }
    }
}

然后在注册方法中

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

        Serializer<List<Name>>.Serialize(@"C:\saved.xml", MusicList);
    }

请注意,您必须在类 Name 中添加默认构造函数

于 2013-03-21T09:42:07.533 回答