Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing program for write text into notepad++ , and this programs works but my question is when my data written in text file it always override the text of first line only, instead I want to write text in newline every-time when program runs. For that I have tried with some code , please have look.

Thanks for any help!

C#
if (res.StartsWith("*"))
{
  using (StreamWriter sw = new StreamWriter(@"C:\Users\MyPC\Desktop\test.txt"))
  {
    sw.WriteLine(res);
    sw.Flush();
    SendKeys.SendWait("{ENTER}");
    SendKeys.Flush();

   }
}


1> In above code res is a string which I am getting from socket and I checked for it and write it in test file. and when this method calls again at 2nd input this input should written in nextline with 1st line data remains constant.

2> In debug the above bold line in code gives error "object reference not set to an instance, nullreferenceException".

Now result :

*abc
and at 2nd time run this overriden by *xyz at same line.

Desired (sample) result:
*abc
*xyz
Posted
Updated 27-Jul-15 20:10pm
v2

You should use an overload of StreamWriter that supports appending to the file.

C#
public StreamWriter(
    string path,
    bool append
)


E.g.
new StreamWriter("file.txt", true /*append to file*/);
 
Share this answer
 
Comments
himanshu agarwal 28-Jul-15 2:17am    
using (StreamWriter sw = new StreamWriter(@"C:\Users\MyPC\Desktop\test.txt", true))
{
sw.WriteLine(res);
sw.Flush();
}
Aravindba 28-Jul-15 2:51am    
5+
Hi When going to write text in notepad,u can add this line

C#
Environment.NewLine

like this

C#
sw.WriteLine(res + Environment.NewLine);

so when write text in notepad Environment.NewLine brings second line,so if u write again in same notepad ,it automatically start from second line.

Or try this link

[^Click]

Instead of buffer u can pass string,i mean your text,


Try this code
C#
string res = "*AAA";
         if (res.StartsWith("*"))
         {
             using (StreamWriter sw = new StreamWriter(@"C:\report.txt",true))
             {
                 sw.WriteLine(res);
                 sw.Flush();


             }
         }



Regards
Aravindb
 
Share this answer
 
v5
Comments
himanshu agarwal 28-Jul-15 2:28am    
This will not work as StreamWriter is being disposed after writing to the file. Next time while writing the second line, new StreamWriter will again point to start of file i.e. line 1.

You would need to seek to the end of file and then write in this case. That is what the overload of StreamWriter does.
himanshu agarwal 28-Jul-15 2:30am    
In essence, WriteLine automatically adds the new line after the text, you need not enter a newline. The reason it is not working is because what I mentioned above.
Aravindba 28-Jul-15 2:31am    
Or u can try to read text form notepad,if text got then add like this
if () //Text got in notepad
sw.WriteLine( Environment.NewLine + res );
else()//if not have text in notepad
sw.WriteLine(res );
Member 11543226 28-Jul-15 2:36am    
this is Not worked , reload not done in notepad and data is still ovveride in same 1st line.
Aravindba 28-Jul-15 2:37am    
https://www.daniweb.com/software-development/csharp/threads/202112/add-text-to-existing-text-file try this code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900