Andy Markman
Andy Markman

Reputation: 127

Read a file into a string in C#

I am trying to read a file into a string which then I will send to another application.

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

namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("c:\text.txt"))
                {
                    string line;

                    // Read and display lines from the file until 
                    // the end of the file is reached. 
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {

                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
    }
}

I am getting the error:

The file could not be read:
Could not find file 'c:\users\andymarkmn\documents\visual studio 2015\Projects\FileApplication\FileApplication\bin\Debug\text.txt'.

I have tried putting the file in bin debug folders as well.

How to make sure the code works ?

EDIT: As suggested, I tried using the different ways.

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

namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath = "c:\\text.txt";

            try
            {
                string lines = File.ReadAllText(filepath);
                Console.Write(lines);
            }
            catch (Exception e)
            {

                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }
    }
}

I still get the error:

The file could not be read:
Could not find file 'c:\text.txt'.

Upvotes: 2

Views: 2096

Answers (4)

Rafael Piernagorda
Rafael Piernagorda

Reputation: 1

Try:

using (StreamReader sr = new StreamReader(@"C:\text.txt"))

If you use c:\text, C# considers that the string has a tabulador between C: and text.txt.

Upvotes: 0

Raktim Biswas
Raktim Biswas

Reputation: 4077

When StreamReader is given a non-qualified path as a parameter, it will look for the file in the application's working directory as you have done:

using (StreamReader sr = new StreamReader("c:\text.txt"))

If the file isn't located there, probably you should give StreamReader a fully qualified path:

using (StreamReader sr = new StreamReader(@"c:\text.txt"))
//or...
using (StreamReader sr = new StreamReader("c:\\text.txt"))

Upvotes: 0

Baldrick
Baldrick

Reputation: 11840

You have accidentally used an unwanted escape sequence in your filename string.

new StreamReader("c:\text.txt")

should be

new StreamReader(@"c:\text.txt")

Otherwise \ gets treated as an escape character, at \t is a tab character. This leaves an unexpected result, and the wrong path for the file.

@ instructs the compiler to ignore any escape characters in the string.

Upvotes: 4

Ryan Searle
Ryan Searle

Reputation: 1627

"c:\text.txt" will not work as \ is an escape character.

use @"c:\text.txt" or "c:\\text.txt"

Upvotes: 0

Related Questions