• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

read from a richtextbox and write to the text file

Hi,

I am trying to read line by line from a richtextbox and write it to the text file.
but the contents on the text files are like
System.String[]
System.String[]
System.String[]
System.String[]
System.String[]
...
What is going on here?  How should I fix this issue?
My codes are follow:

string myPath = @"C:\\MyFolder" + "myTextFile.txt";

StreamWriter sw = File.CreateText(myPath);
int count = myRichTbx.Lines.Length;

for (int i = 0; i < count; i++)
{
      sw.WriteLine(myRichTbx.Lines.ToString());    
 }
sw.Close();
0
IzzyTwinkly
Asked:
IzzyTwinkly
  • 2
1 Solution
 
abelCommented:
This line:

string myPath = @"C:\\MyFolder" + "myTextFile.txt";
is probably not what you want it to be. It goes to C:\\MyFolder instead of C:\MyFolder. Try this instead:

string myPath = @"C:\MyFolder" + "myTextFile.txt";

The reason that you System.String[] is because you are converting the Lines array to a string. Try:






string myPath = @"C:\MyFolder" + "myTextFile.txt";
 
StreamWriter sw = File.CreateText(myPath);
int count = myRichTbx.Lines.Length;
 
for (int i = 0; i < count; i++)
{
      sw.WriteLine(myRichTbx.Lines[i]);    
}
sw.Close();

Open in new window

0
 
abelCommented:
It's even easier if you use foreach:

string myPath = @"C:\MyFolder\" + "myTextFile.txt";
 
StreamWriter sw = File.CreateText(myPath);
 
for (string sLine in myRichTbx.Lines)
{
      sw.WriteLine(sLine);    
}
sw.Close();

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now