簡體   English   中英

從同一文本框中讀取和寫入文件

[英].Reading and writing to a file from the same textbox

我有一個Windows窗體,可以從文件中讀取字符串,並在按下按鈕時將其全部顯示在文本框中。

private void buttonTxt_Click(object sender, EventArgs e)
{
    string[] Test = File.ReadAllLines("C:\\testfile.txt");
    for (int i = 0; i < testfile.Length; i++)
    {
        TextBox.Text += testfile[i];
    }
}

我想做兩個單選按鈕。 因此,第一個按鈕使我的程序按我描述的方式工作(默認情況下),第二個單選按鈕使它反之亦然- 這樣我可以自己在文本框中編寫內容,當我按下按鈕時,它會在同一行中寫一行新行文件 有辦法嗎?

只需在此事件處理程序中添加if語句,即可實現發送和接收數據。 完成。 樣品原理:

private const string FilePath = @"C:\testfile.txt";

private void buttonTxt_Click(object sender, EventArgs e)
{
    if (radioReadMode.Checked) // check which radio button is selected
    {   // read mode
        string[] Test = File.ReadAllLines(FilePath);
        for (int i = 0; i < testfile.Length; i++)
            TextBox.Text += testfile[i];
    }
    else
    {   // write mode
        File.WriteAllText(FilePath, TextBox.Text);
    }
}

我相信這就是您想要的。 如果選中單選按鈕1,則如果文件存在,它將讀取該文件並將其放入表單的文本框中。 如果切換到單選按鈕2,則可以在文本框中鍵入內容,然后在按下按鈕時將其附加到文件中。

public partial class Form1 : Form
{
    System.IO.StreamReader sr;
    System.IO.StreamWriter sw;

    public Form1()
    {
        InitializeComponent();
        radioButton1.Checked = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (radioButton1.Checked == true)
        {
            if (System.IO.File.Exists("C:\\testfile.txt"))
            {

                try
                {
                    sr = new System.IO.StreamReader("C:\\testfile.txt");
                    while (!sr.EndOfStream)
                    {
                        textBox1.Text += sr.ReadLine() + "\r\n";
                    }
                }
                finally 
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
        }

        if (radioButton2.Checked == true)
        {
            if (System.IO.File.Exists("C:\\testfile.txt"))
            {
                try
                {
                    sw = new System.IO.StreamWriter("C:\\testfile.txt", true);
                    string result = textBox1.Text;
                    string[] lststr = result.Split(new Char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string s in lststr)
                    {
                        sw.WriteLine(s);
                    }
                }
                finally 
                {
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        textBox1.Clear();
        textBox1.ReadOnly = true;

    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        textBox1.Clear();
        textBox1.ReadOnly = false;
    }


}

是的,有辦法。 只需在表單中添加另一個按鈕,然后在內部從文本框中讀取文本即可:

var textBoxContent = TextBox.Text;

並以這種方式將其保存到您的文件中

File.WriteAllText("C:\\testfile.txt", textBoxContent);

注意:我建議將文件路徑放入變量中

暫無
暫無

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

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