Question:
how do i include multiple lines to a file thru console appln in c#.net?
Alonso
2010-04-28 12:21:55 UTC
i want to add multiple lines of text to a file( say in C:\abc.txt) from console application using c#.net? how do i do that ? i am able to do add a single line of text but mulitple lines i can't.. please help me out guys
Three answers:
PalmTrees
2010-05-01 07:01:39 UTC
Hi, I hope to be useful for you:



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;



namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{



try

{



///////////////////////Starting writing to a file ///////

// Create an instance of StreamWriter to write text to a file.

// The using statement also closes the StreamWriter.

using (StreamWriter sw = new StreamWriter("mytextfile.txt"))

{

// Add some text to the file.

sw.Write("This is the ");

sw.WriteLine("header for the file.");

sw.WriteLine("-------------------");

// Arbitrary objects can also be written to the file.

sw.Write("The date is: ");

sw.WriteLine(DateTime.Now);

}

////////////////////////////////////end//////////////////////////



///////////////////////Starting reading from a file ///////

// Create an instance of StreamReader to read from a file.

// The using statement also closes the StreamReader.

//using (StreamReader sr = new StreamReader("TestFile.txt"))

using (StreamReader sr = new StreamReader("mytextfile.txt"))



{

string line;

// Read and display lines from the file until the end of

// the file is reached.

while ((line = sr.ReadLine()) != null)

{

Console.WriteLine(line);

}

}

}

catch (Exception e)

{

// Let the user know what went wrong.

Console.WriteLine("The file could not be read:");

Console.WriteLine(e.Message);

}



}

}

}
?
2016-12-04 11:34:12 UTC
humorous even however a number of them does not paintings, a supervisor is often wearing a headset as properly, so he will pay attention that your conversing a remote places laguage, and the microphone element wouldnt' paintings the two by way of fact they might't pay attention you as they're conversing...kinda like a walkie talkie does...you may in basic terms do one after the different..... yet I incredibly have long previous by using backwards incredibly some cases...it somewhat is humorous and persons have surpassed me rubbish quite some cases
2010-04-29 12:27:14 UTC
Please include your code for a single line in your question. Then we will help you to modify it for multiple lines.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...