如何检索文件数据(.txt)到相应的文本框?

时间:2014-05-15 10:09:25

标签: c# winforms

在我的代码中我正在使用保存文件对话框将文本框数据写入文本文件,这会将我的文本框数据保存到指定的文本文件。我的问题是我需要将文件数据检索到相应的文本框时用户需要......我该怎么做?

  private void SaveData_Click(object sender, EventArgs e)
    {
       // Stream myStream;
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        saveFileDialog1.InitialDirectory = "c:\\";
        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((myStream = saveFileDialog1.OpenFile()) != null)
            {
                // Code to write the stream goes here.
                using (StreamWriter objWriter = new StreamWriter(myStream))
                {
                    objWriter.Write(textBox1.Text);
                    objWriter.Write(","); 
                    objWriter.Write(textBox2.Text);
                    objWriter.Write(",");
                    objWriter.Write(textBox3.Text);
                    objWriter.Write(",");
                    objWriter.Write(textBox4.Text);

                    MessageBox.Show("Details have been saved");
                }


                myStream.Close();
            }
        }
    }

    private void Retrieve_Click(object sender, EventArgs e)
    {
        //Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "c:\\";
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        // Insert code to read the stream here.
                        textBox1.Text = (myStream).ToString();
                      textBox2.Text = ().ToString();
                      textBox3.Text = ().Tostring();
                      textBox4.text = ().Tostring();

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

您需要使用OpenFileDialog控件并将其传递给ReadAllText方法..

这是一个例子:

    myAmazingTextBox.Text = File.ReadAllText(openFileDialog1.FileName);

答案 1 :(得分:0)

保存用户保存数据的位置。\ 下次你应该首先阅读路径。 您可以保存saveFileDialog1.FileName

答案 2 :(得分:0)

使用以下代码检索文件中保存的文本(在try块中更改的代码)

private void Retrieve_Click(object sender, EventArgs e)
{
    //Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\";
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            // Read all text stored in the file
            string fileData = File.ReadAllText(openFileDialog1.FileName);

            // As you are appending textbox data using comma as separator, 
            // so split the text read from file on comma separator
            string[] parts = fileData.Split(','); 

            // as there were 4 textboxes, so after split, the 'parts' array should contain 4 elements, otherwise, the file/data is invalid
            if(parts.Length != 4)
            {
                 MessageBox.Show("Invalid source file.");
                 return;
            }

            // set the respective values into the textboxes 
            textBox1.Text = parts[0];
            textBox2.Text = parts[1];
            textBox3.Text = parts[2];
            textBox4.text = parts[3];
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }

我建议您不要使用逗号作为分隔符,因为用户可以在内容中输入包含逗号的文本。我建议您将文本编码为Base64字符串(其中仅包含A-Za-z0-9,其中包含两个不包含逗号的字符。因此,您可以将base64编码的字符串与逗号分隔符分开,然后您将为100确定逗号只是分隔符而不是内容的一部分。

在阅读时,解码base64字符串并显示到文本框中。