Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Reading a txt file in C#

+3 votes
asked Feb 13, 2023 by Valerie Yen (150 points)
Hi! I'm learning C# for a class I'm in and my teacher wants us to use ASCII art for one of these very basic like assignments. I put it in to a txt file in the same project but I don't know the syntax for opening it in my main.cs

1 Answer

+1 vote
answered Feb 15, 2023 by Peter Minarik (86,160 points)

You can read the whole content of the file in one go and print it out to the console.

The following little code reads the content of the very same source file that was run. Just replace the file with your file that contains the ASCII art:

using System;
using System.IO;

class HelloWorld
{
  static void Main()
  {
    Console.WriteLine(File.ReadAllText("main.cs"));
  }
}
commented Apr 14, 2023 by AAAitsAxxelbro Reyes (100 points)
Wow thankyou I was also wondering the same thing
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...