You need to use an OpenFileDialog Control and pass it to the ReadAllText Method ..
Here is an Example :
myAmazingTextBox.Text = File.ReadAllText(openFileDialog1.FileName);
Pergunta
in my code i am writing text boxes data to a text file using save file dialog, which will save my text box data to a specified text file.and my problem is i need to retrieve back file data to respective text boxes when ever user required ...how can i do it?
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);
}
}
}
Solução
You need to use an OpenFileDialog Control and pass it to the ReadAllText Method ..
Here is an Example :
myAmazingTextBox.Text = File.ReadAllText(openFileDialog1.FileName);
Outras dicas
Save the location where user saves data.\
Next time you should read the path first.
You can save the saveFileDialog1.FileName
.
Use the code below to retrieve the text saved in the file (code changed within try block)
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);
}
}
I would like to suggest that you don't use comma as a separator as the user may enter text which contains comma in the content. I would suggest that you encode the text into Base64 string (which contains only A-Za-z0-9 with additional two characters which does not contain comma. So, you can separate the base64 encoded string with comma separator as then you will be 100% sure that comma is only the separator and not the part of content.
While reading, decode the base64 string and show into textboxes.