6 Replies - 494 Views - Last Post: 28 September 2011 - 10:55 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Writing Multiples Lines Of Text To a Text File, Over And Over Again?

Posted 27 September 2011 - 10:46 PM

So I need a little here fellas. My IP Program is working out great, but one of it's features is writing a line of
your IP when it changes. Ok so here's the problem. I am sure I am missing some little detail. If you use


File.WriteAllText("Date.txt", DateTime.Now.ToString());
            File.Exists("Date.Txt");




This will write the date to that file if it exists, and that's all fine and dandy but it WON'T write it again on ANOTHER LINE once this code is executed again either by calling a function or opening the file again. Also,


Textwriter _WriteDate = new StreamWriter(Date.txt);

_WriteDate.WriteLine(DateTime.Now);

_WriteDate.Close();





Will also create the file and write that same line of text, but again it suffers from the same problem it won't write the line again even calling it in a function like.

public void WriteDateToLog()
{
Textwriter _WriteDate = new StreamWriter(Date.txt);

_WriteDate.WriteLine(DateTime.Now);

_WriteDate.Close();

}





Still won't work if it's called throughout the program. What am I doing wrong and how can you go about doing this?

Thank you all in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Writing Multiples Lines Of Text To a Text File, Over And Over Again?

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4455
  • View blog
  • Posts: 7,760
  • Joined: 08-June 10

Re: Writing Multiples Lines Of Text To a Text File, Over And Over Again?

Posted 27 September 2011 - 11:13 PM

StreamWriter's constructor has an overload with a second parameter, a bool called "append." If you set this to true, it will open and seek to the end of the stream.

Also, I suggest you look into the using statement. For classes that implement IDisposable, it automatically disposes of objects created by it when it leaves scope. So if you create your streamwriter with one, it'll flush, close, and dispose of it automatically.
Was This Post Helpful? 1
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Writing Multiples Lines Of Text To a Text File, Over And Over Again?

Posted 28 September 2011 - 12:26 AM

View PostCurtis Rutland, on 27 September 2011 - 11:13 PM, said:

StreamWriter's constructor has an overload with a second parameter, a bool called "append." If you set this to true, it will open and seek to the end of the stream.

Also, I suggest you look into the using statement. For classes that implement IDisposable, it automatically disposes of objects created by it when it leaves scope. So if you create your streamwriter with one, it'll flush, close, and dispose of it automatically.


Thank you very much :)
Was This Post Helpful? 0
  • +
  • -

#4 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Writing Multiples Lines Of Text To a Text File, Over And Over Again?

Posted 28 September 2011 - 01:44 AM

View PostCurtis Rutland, on 27 September 2011 - 11:13 PM, said:

StreamWriter's constructor has an overload with a second parameter, a bool called "append." If you set this to true, it will open and seek to the end of the stream.

Also, I suggest you look into the using statement. For classes that implement IDisposable, it automatically disposes of objects created by it when it leaves scope. So if you create your streamwriter with one, it'll flush, close, and dispose of it automatically.




Oh So one more question about this. The append certainly works fine but in terms of cleaning up objects and resources etc.

 using (StreamWriter IPLog = File.AppendText("IPLog.txt"))
            {

                IPLog.WriteLine(DateTime.Now.ToString() + " Is The Date Of The IP Change.. Your Current New Recorded IP To Log Is..  " + _IPTxtBox.Text);

               
                
              
            }




In The example above you technically would NOT have to use IPLog.Dispose(); and IPLog.Close(); Right? As this is done essentially for you?
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Writing Multiples Lines Of Text To a Text File, Over And Over Again?

Posted 28 September 2011 - 06:21 AM

I'm glad Curtis Rutland was able to help with your problem

Don't forget that we all get rated on how much we can help.
So if anyone is able to help you out on any of your questions like this, don't forget to hit that green + button in the lower-right corner of their post that helped you. That gives the person 1 more good reputation point.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4455
  • View blog
  • Posts: 7,760
  • Joined: 08-June 10

Re: Writing Multiples Lines Of Text To a Text File, Over And Over Again?

Posted 28 September 2011 - 06:34 AM

View Postadn258, on 28 September 2011 - 03:44 AM, said:

Oh So one more question about this. The append certainly works fine but in terms of cleaning up objects and resources etc.

 using (StreamWriter IPLog = File.AppendText("IPLog.txt"))
            {

                IPLog.WriteLine(DateTime.Now.ToString() + " Is The Date Of The IP Change.. Your Current New Recorded IP To Log Is..  " + _IPTxtBox.Text);
              
            }




In The example above you technically would NOT have to use IPLog.Dispose(); and IPLog.Close(); Right? As this is done essentially for you?


Right. The using block creates and manages an object that implements IDisposable. It's good to check documentation to make sure that the object's Dispose method does what you'd expect it to do, but in the case of streams, it flushes and closes them. You can use this for database connections, file IO, basically anything that is Disposable.
Was This Post Helpful? 0
  • +
  • -

#7 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Writing Multiples Lines Of Text To a Text File, Over And Over Again?

Posted 28 September 2011 - 10:55 AM

View PosttlhIn`toq, on 28 September 2011 - 06:21 AM, said:

I'm glad Curtis Rutland was able to help with your problem

Don't forget that we all get rated on how much we can help.
So if anyone is able to help you out on any of your questions like this, don't forget to hit that green + button in the lower-right corner of their post that helped you. That gives the person 1 more good reputation point.



View PostCurtis Rutland, on 28 September 2011 - 06:34 AM, said:

View Postadn258, on 28 September 2011 - 03:44 AM, said:

Oh So one more question about this. The append certainly works fine but in terms of cleaning up objects and resources etc.

 using (StreamWriter IPLog = File.AppendText("IPLog.txt"))
            {

                IPLog.WriteLine(DateTime.Now.ToString() + " Is The Date Of The IP Change.. Your Current New Recorded IP To Log Is..  " + _IPTxtBox.Text);
              
            }




In The example above you technically would NOT have to use IPLog.Dispose(); and IPLog.Close(); Right? As this is done essentially for you?


Right. The using block creates and manages an object that implements IDisposable. It's good to check documentation to make sure that the object's Dispose method does what you'd expect it to do, but in the case of streams, it flushes and closes them. You can use this for database connections, file IO, basically anything that is Disposable.



Thank you for verifying and thanks for all your help. In terms of helping others I definitely want too. I'm getting better with programming but I still have a lot to learn. As time goes on I want to help that need it, including on here.

Peace
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1