簡體   English   中英

寫入以逗號分隔的文本文件

[英]Writing to a comma delimited text file

我很難找到如何寫入以逗號分隔的文本文件。 我正在為自己創建一個非常基本的地址表格。 我想當我單擊button1時,它會創建一個文本文件,然后將textbox1,textbox2,textbox3和maskedtextbox1中的數據寫入到以逗號分隔的文件中。

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private void close_Click(object sender, EventArgs e)
    {
        Close();
    }
}

 }

創建csv文件非常容易。 請嘗試以下操作:

string s1 = TextBox1.Text;
string s2 = TextBox2.Text;
string s3 = TextBox3.Text;
string s4 = maskedtextbox1.Text;

using (StreamWriter sw = new StreamWriter("C:\\text.txt", true))  // True to append data to the file; false to overwrite the file
{
    sw.WriteLine(string.Format("[0],[1],[2],[3]", new object[] { s1, s2, s3, s4 }));
}

或者,如果您不喜歡string.Format方法,則可以執行以下操作:

using (StreamWriter sw = new StreamWriter("C:\\text.txt", true))
{
    sw.WriteLine(s1 + "," + s2 + "," + s3 + "," + s4}));
}

您將需要使用兩個類:FileStream和StreamWriter。 也許這份文件 但由於我懷疑這是一項家庭作業,因此我樂於提供更多幫助。 您應該能夠很容易地弄清楚它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM