Hello,
I am new to C# and Visual C#. I am writing an application that reads text from a file into a textbox, then I will search the text for a particular to count the number of times that word exists, then save the modified text in a file. I was able to get the input into the textbox to work right. But, now I am working on saving the textbox text into a file. I was able to get this to work with the write method, but there weren't any line breaks. I need to preserve the line breaks.
I have tried to modify the code to use the writeline method, but nothing I have used seems to work. I am trying to modify the code I have that loaded the text into the textbox from the opened file, but it just isn't working.
Can anyone give me a suggestion on what I am missing?
Here is the code I have for the saveFileMenu_Click that worked but didn't preserve the line breaks:
Code:
private void saveFileMenu_Click(object sender, RoutedEventArgs e)
{
saveFileDialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
saveFileDialog.FileName = targetWord.Text.ToUpper() + "_" + fileName.Text;
saveFileDialog.DefaultExt = "txt";
saveFileDialog.AddExtension = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.Title = "Word Count Save As";
saveFileDialog.ValidateNames = true;
if (saveFileDialog.ShowDialog().Value)
{
StreamWriter writer = new StreamWriter(saveFileDialog.FileName);
writer.Write(targetDocument.Text);
writer.Close();
MessageBox.Show("THE FILE: \n\n'" + saveFileDialog.FileName + "'\n\nHAS BEEN SAVED!", saveFileDialog.Title);
}
}
This is what I am trying to mimic from the openFileDialog_Click which loads the textbox, but I can't get it to work:
Code:
targetDocument.Text = "";
TextReader reader = source.OpenText();
string rLine = reader.ReadLine();
while (rLine != null)
{
targetDocument.Text += rLine + "\n";
rLine = reader.ReadLine();
}
reader.Close();
But it just isn't coming together.
I would appreciate a little electricity for the light bulb that is over my head that isn't as bright as it should be. (My attempt at a little stupid humor!).
Thanks...