Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

file

notepad

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

like image 800
ghie Avatar asked Jun 02 '11 20:06

ghie


3 Answers

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.  
        }

    }
like image 108
Ken Pespisa Avatar answered Oct 06 '22 00:10

Ken Pespisa


You need to read the content of the file, e.g.:

using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read))
{
    return reader.ReadToEnd();
}

Or, as simply as possible:

return File.ReadAllText(path);
like image 24
Matthew Abbott Avatar answered Oct 06 '22 01:10

Matthew Abbott


make use of StreamReader and read the file as shown below

string notepad = @"c:\oasis\B1.text";
StringBuilder sb = new StringBuilder();
 using (StreamReader sr = new StreamReader(notepad)) 
            {
                while (sr.Peek() >= 0) 
                {
                    sb.Append(sr.ReadLine());
                }
            }

string s = sb.ToString();
like image 23
Pranay Rana Avatar answered Oct 06 '22 01:10

Pranay Rana