如何检索文件数据(.txt)到各自的文本框
本文关键字:文本 数据 何检索 检索 文件 txt | 更新日期: 2023-09-27 18:03:24
在我的代码中,我使用保存文件对话框将文本框数据写入文本文件,这将保存我的文本框数据到指定的文本文件。我的问题是,我需要检索回文件数据到各自的文本框时,用户需要…我该怎么做呢?
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);
}
}
}
您需要使用OpenFileDialog控件并将其传递给ReadAllText方法。
下面是一个例子:
myAmazingTextBox.Text = File.ReadAllText(openFileDialog1.FileName);
保存用户保存数据的位置下次你应该先阅读路径。您可以保存saveFileDialog1.FileName
.
使用下面的代码检索保存在文件中的文本(在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字符串并显示到文本框中