5

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

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
ghie
  • 567
  • 4
  • 11
  • 26

6 Answers6

7

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

    }
Ken Pespisa
  • 21,989
  • 3
  • 55
  • 63
  • This isn't correct, you have a race condition between your call to File.Exists and actually reading the file. If the file is deleted in that period, your solution will crash. – Greg D Jun 03 '11 at 13:25
  • @Greg D, that's a bit nitpicky, don't you think? Is there anything about this question that makes you think the code needs to be that bullet proof? – Ken Pespisa Jun 03 '11 at 13:46
  • @Greg D, on second thought, I stumbled onto [your answer here](http://stackoverflow.com/questions/4509415/check-whether-a-folder-exists-in-a-path-in-c/4509517#4509517) and like that a lot. Thanks! I've updated my answer. (I still disagree with the downvote though :) – Ken Pespisa Jun 03 '11 at 14:11
  • In cases like this, yes, the code should be that bullet proof. Especially because it's so easy to be so, and that nit-pick could easily be a security vulnerability in your codebase. :) – Greg D Jun 11 '11 at 18:02
6

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);
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
5

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();
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
5

Use File.ReadAllText

string text_in_file = File.ReadAllText(notepad);
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
3

Reading From a Text File (Visual C#), in this example @ is not used when StreamReader is being called, however when you write the code in Visual Studio it will give the below error for each \

Unrecognized escape sequence

To escape this error you can write @ before " that is at the beginning of your path string. I shoul also mentioned that it does not give this error if we use \\ even if we do not write @.

// Read the file as one string.
System.IO.StreamReader myFile = new System.IO.StreamReader(@"c:\oasis\B1.text");
string myString = myFile.ReadToEnd();

myFile.Close();

// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();
Bastardo
  • 4,144
  • 9
  • 41
  • 60
3

check this example:

// Read the file as one string.
System.IO.StreamReader myFile =
   new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();

myFile.Close();

// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();
BlackKnights
  • 135
  • 1
  • 9