get the value of notepad and put it inside the c# string?

ghie picture ghie · Jun 2, 2011 · Viewed 7.5k times · Source

Notepad:

Hello world!

How I'll put it in C# and convert it into string..?

So far, I'm getting the path of the notepad.

 string notepad = @"c:\oasis\B1.text"; //this must be Hello world

Please advice me.. I'm not familiar on this.. tnx

Answer

Ken Pespisa picture Ken Pespisa · Jun 2, 2011

You can read text using the File.ReadAllText() method:

    public static void Main()
    {
        string path = @"c:\oasis\B1.txt";

        try {

            // Open the file to read from.
            string readText = System.IO.File.ReadAllText(path);
            Console.WriteLine(readText);

        }
        catch (System.IO.FileNotFoundException fnfe) {
            // Handle file not found.  
        }

    }