-1

I want to write console input Console.ReadLine to a text file.

It is hard find it anywhere.

Any help is appreciated.

Gible
  • 156
  • 3
  • 19
  • is file already created? – Vivek Nuna Jun 12 '20 at 06:57
  • 3
    Show us your code and the errors and we can then help you. Rather than just give you code that you might not understand (You can google for that!) – jason.kaisersmith Jun 12 '20 at 06:58
  • yes it is already created – Gible Jun 12 '20 at 06:58
  • 2
    `System.IO.File.WriteAllText(@"C:\test.txt", Console.ReadLine());` – fubo Jun 12 '20 at 07:00
  • "It's hard to find anywhere"... apart from the Microsoft website, which is the first google result: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file – George Kerwood Jun 12 '20 at 07:06
  • If the file is already created, instead the `WriteAllText` you should call [File.AppendAlLText](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.appendalltext?view=netframework-4.7.2#System_IO_File_AppendAllText_System_String_System_String) – Cleptus Jun 12 '20 at 07:08
  • 3
    "_It is hard find it anywhere._". Take your previous sentence. remove all necessary words. " ̶I̶ ̶w̶a̶n̶t̶ ̶t̶o̶ **write** ̶c̶o̶n̶s̶o̶l̶e̶ ̶i̶n̶p̶u̶t̶ ̶(̶C̶o̶n̶s̶o̶l̶e̶.̶R̶e̶a̶d̶L̶i̶n̶e̶)̶ ̶t̶o̶ ̶a̶ **text file**". Add the language. And "Write text file C#" in any order will give you good result on any search engigne. Like [How to write to a text file (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file) – Drag and Drop Jun 12 '20 at 07:28
  • 1
    thank you for all of the great answers, I used this answer: System.IO.File.WriteAllText(@"C:\test.txt", Console.ReadLine()); – Gible Jun 12 '20 at 07:30

2 Answers2

5

Since you didn't tell whether the text file already exists or not, you may want-

string path = @"C:\Users\power\Desktop\Tor Browser\test.txt";

if (!File.Exists(path))
{
    using FileStream fs = File.Create(path);
}

The code above checks whether file exists or not; if no, creates the file.
If you want append text to file, not overwrite:

string path = @"C:\Users\power\Desktop\Tor Browser\test.txt";
File.AppendAllText(path, "some text");

If you want append text to file in a new line, not overwrite:

string path = @"C:\Users\power\Desktop\Tor Browser\test.txt";
File.AppendAllText(path, Environment.NewLine + "some text");

If you want overwrite the text file:

string path = @"C:\Users\power\Desktop\Tor Browser\test.txt";
File.WriteAllText(path, "some text");

If you have name of the file separated:

string sepratedPath = @"C:\Users\power\Desktop\Tor Browser"
string sepratedFileName = "test.txt";

string path = Path.Combine(sepratedPath, sepratedFileName);

If you want append items of an array line by line:

string path = @"C:\Users\power\Desktop\Tor Browser\test.txt";
string[] lines = { "New line 1", "New line 2" };
File.AppendAllLines(path, lines);

Do not forget using System.IO;.

My research:

0

You can try this:

using (System.IO.FileStream fs = new System.IO.FileStream([file-path], System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite))
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fs))
    writer.WriteLine(Console.ReadLine());

thank u,