Hello all,
I've made a program using gui to open the content from a .TXT file in a richTextBox and to save it in another .TXT file
My GUI has
2 butons + 1 richTextBox(OpenFile, SaveFile, richTextBox)
- when I click OpenFile button a dialog window launch and ask for a .TXT file and put the content from that file in my richTextBox
Test 1 -- on the first line
Test 2 -- on the second line
When I click SaveFile button launch the save window a I select or create a .TXT file to save the content from my richTextBox
**** ==== MY Problem
When I save the content in the file .TXT the 2 separate lines from my richTextBox are putted on the same line in the file (Test1Test2) and I want to be Test1 on the first line and Test 2 on my second line
***** ====
My code:
private void OpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "*.txt|";
if (openFile.ShowDialog()== DialogResult.OK)
{
try
{
FileStream file = new FileStream(deschide.FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
byte[] info = new byte[file.Length];
file.Read(info,0,info.Length);
richTextBox1.Text = System.Text.Encoding.ASCII.GetString(info);
catch (Exception eror)
{
MessageBox.Show(eror.Message);
}
finally
{
file.Close();
}
}
======== end of open file code ====================
private void SaveFile_Click(object sender, EventArgs e)
{
SaveFileDialog DialogSave = new SaveFileDialog();
DialogSave.Filter = "Text file (*.txt)|*.txt";
if (DialogSave.ShowDialog() == DialogResult.OK)
{
try
{
FileStream file = new FileStream(openFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
foreach (string line in richTextBox1.Lines)
{
byte[] infoscrie = System.Text.Encoding.ASCII.GetBytes(line);
file.Write(infoscrie, 0, infoscrie.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
file.Close();
}
}
Thanks
I've made a program using gui to open the content from a .TXT file in a richTextBox and to save it in another .TXT file
My GUI has
2 butons + 1 richTextBox(OpenFile, SaveFile, richTextBox)
- when I click OpenFile button a dialog window launch and ask for a .TXT file and put the content from that file in my richTextBox
Test 1 -- on the first line
Test 2 -- on the second line
When I click SaveFile button launch the save window a I select or create a .TXT file to save the content from my richTextBox
**** ==== MY Problem
When I save the content in the file .TXT the 2 separate lines from my richTextBox are putted on the same line in the file (Test1Test2) and I want to be Test1 on the first line and Test 2 on my second line
***** ====
My code:
private void OpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "*.txt|";
if (openFile.ShowDialog()== DialogResult.OK)
{
try
{
FileStream file = new FileStream(deschide.FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
byte[] info = new byte[file.Length];
file.Read(info,0,info.Length);
richTextBox1.Text = System.Text.Encoding.ASCII.GetString(info);
catch (Exception eror)
{
MessageBox.Show(eror.Message);
}
finally
{
file.Close();
}
}
======== end of open file code ====================
private void SaveFile_Click(object sender, EventArgs e)
{
SaveFileDialog DialogSave = new SaveFileDialog();
DialogSave.Filter = "Text file (*.txt)|*.txt";
if (DialogSave.ShowDialog() == DialogResult.OK)
{
try
{
FileStream file = new FileStream(openFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
foreach (string line in richTextBox1.Lines)
{
byte[] infoscrie = System.Text.Encoding.ASCII.GetBytes(line);
file.Write(infoscrie, 0, infoscrie.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
file.Close();
}
}
Thanks