I try to make a program that read the content from a .txt file and then print the content using Console.WriteLine() .I add the file that I want to read to my project .
This is the content of my file:
Hello.txt:

Hello World!!!!!!!!!!!!!

And this is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileIO
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream file = new FileStream("HELLO.txt",FileMode.Open,FileAccess.Read);
            StreamReader str = new StreamReader(file);
            string hello = str.ReadToEnd();
            Console.WriteLine(hello);
            Console.ReadLine();
            str.Close();

        }
    }
}

My problem is that when i run the project is nothing appearing .
Please , say to my which is the problem and i will be thankful .

Edited 6 Years Ago by paul9998: n/a

You didn't proved the full path of "HELLO.txt". Lets say, if its located in "C:\" then use this:

If so, then use this:

FileStream(@"C:\HELLO.txt", FileMode.Open,FileAccess.Read);

Or if its located in the same path as the executable. Then use this:

FileStream file = new FileStream(Path.Combine(Application.StartupPath, "HELLO.txt"), FileMode.Open,FileAccess.Read);

Also, if you only want to read the txt then you can use an alternative way:

string hello = File.OpenText(Path.Combine(Application.StartupPath, "HELLO.txt")).ReadToEnd()

Thanks

Edited 6 Years Ago by farooqaaa: n/a

This article has been dead for over six months. Start a new discussion instead.