Hi,
you are doing a lot of things wrong in your code above.
1) Reading a text from from a text box into file without checking for empty string.
2) Reading the text box text into string array;
3) Checking the length of the array as size: this will be 1 in this case as only one text box has been read.
4) Opening a file with File "OpenCreate" this means if the file doesn't exists, then create one; then reading the file with stream reader, this is unnecessary.
5) For loop with "Size", if the text box was empty, then the size will be 0, if not then it will be 1; however;
6) Reading a line from the file that was opened with "OpenCreate", if the file was just created, it will return null; and this is converted to int32, this will throw exception that has not been caught.
7) Again using FileStream, FileCreate and StreamWriter; totally unnecessary.
Have you consider the code below:
public void WriteToFile()
{
string fileName_1 = "memory.txt";
string fileName_2 = "dest.txt";
List<string> readIn = new List<string>();
FileInfo fi = new FileInfo(fileName_1);
if (!fi.Exists)
{
MessageBox.Show("Memory file doesn't exists!");
return;
}
else
{
using (StreamReader sr = new StreamReader(fileName_1, System.Text.Encoding.Default))
{
string str = "";
while ((str =sr.ReadLine()) != null)
{
readIn.Add(str);
}
}
}
if (readIn.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (string str in readIn)
{
sb.Append(str + "\r");
}
using (TextWriter textWriter = new StreamWriter(fileName_2, true))
{
textWriter.Write(sb.ToString());
}
}
}
Regards
Jegan