I have been trying to write some strings to the notepad.
I have successfullly written the first string , however when I try to add another string,It always over writes the first string I have written.Can you tell me a way where I can write strings to the next line all the time I want to write to the note pad

thanks

Recommended Answers

All 10 Replies

//use the WriteLine method of the StreamWriter class...like so...

using (StreamWriter mywriter = new StreamWriter(//your file path here))
{
mywriter.WriteLine("LINE 1");
mywriter.WriteLine("LINE 2");
}

//or you could put values in an array and write each one using a foreach loop...

string[] lines = { "LINE 1", "LINE 2", "LINE 3" };

            using (StreamWriter mywriter = new StreamWriter(//your file path here))
            {
                foreach (string s in lines)
                {
                    mywriter.WriteLine(s);
                } 
            }

Hope it helps

Here you have simple examples of using StreamWriter, which is in your case by far best object to use.
And here you can read about StreamWriter class. Its good to know which metods and properties to use with this object.
Hope it helps,
Mitja

The problem that i am facing is , everytime I write using streamwriter , It overwrites the existing line in the note pad ,But doesnt start from the next line.How should I tell the stream writer from where it should start.I want each input to be in a seperate line.

If I understand you, you would like to write into a file constantly, every time simply adding lines on the end of the current text? Is that correct?

If so, you can use File.AppendText method. This how:

using (StreamWriter sw = File.AppendText(path)) 
        {
            sw.WriteLine("This");
            sw.WriteLine("is Extra");
            sw.WriteLine("Text");
        }

Hope this helps,
Mitja

Hi, also got a streamwriter problem, anybody mind if I kinda hijack this thread?

What problems? You cannot access to this class?
Add a new namespace "IO" (using System.IO;).

If this is not it, please tell me exactly which problem is it.
Mitja

@jay_el_em
Yes I mind. Post YOUR code and YOUR problem in a different thread. We all be glad to help:)

@ddanbe
Will do that, and I know I should read the code of conduct, so I probably will now. Thanks

before you leave, mark this thread as answered (in case if you got th answer on the thread topic).
best regards
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.