Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to save in a .TXT file same as in richTextBox (C#)

Status
Not open for further replies.

Alric2rei

Programmer
Jul 17, 2009
4
RO
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




 
pretty sure this is the problem
Code:
foreach (string line in richTextBox1.Lines)
{
   var infoscrie = Encoding.ASCII.GetBytes(line);
   file.Write(infoscrie, 0, infoscrie.Length);
}
richTextBox1.Lines most likely removes the line breaks (which it users internally to create the collection).
try something like this instead
Code:
File.Create("full path to file.txt", richTextBox1.Text);

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thank you (Jason Meckley) for your reply I tried and didn't work but I figured out how to do it and it work

For anybody who will have the same problem, they must change SaveFile to:

SaveFileDialog DialogSave = new SaveFileDialog();
DialogSave.ShowDialog();
string path = DialogSave.FileName;
StreamWriter writer = new StreamWriter(path);

try
{
foreach (string linie in richTextBox1.Lines)
{
writer.Write(linie + Environment.NewLine);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

finally
{
writer.Close();
}

And it should work

The Enviromwent.NewLine I was searching for





 
couldn't you have done this:
Code:
StreamWriter sw = null;

try {
   sw = new StreamWriter(@"path\to\file.ext");
   sw.Write(richTextBox1.Text);
}
catch(IOException exc){
  // handle it here
}
finally {
  sw.Flush();
  sw.Close();
}

Regards,

Martin

Computing Design And Services:
 
Thanks for your reply Martin but if I would done it like that it would have put all my lines from richTextBox on a single line in my txt file.

I mean :

Let say that I would have this lines in richTextBox
TEST 1
TEST 2
TEST 3

When I would save it in my TXT file I would have:
TEST 1TEST 2TEST 3

Thanks


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top