简体   繁体   中英

How to to read data from text file in c#?

I want to read data from text file I tried but it showing errors showing error at the path of the file

string txtfile = File.ReadAllText("D:\Temp\textdata.txt");
string txtdata = File.ReadAllText("D:\Temp\textstrings.txt");
string txtpara = File.ReadAllText("D:\Temp\textlines.txt");

Console.WriteLine(txtfile);
Console.WriteLine("\n");
Console.WriteLine("\n");
Console.WriteLine(txtpara);
Console.WriteLine("\n");
Console.WriteLine("\n");
Console.WriteLine("\n");
Console.WriteLine(txtdata);

My file is saved in d:\\temp\\textdata.txt

Can anyone tell me ?

The problem is backslash symbol in your string containing filename. Sequence of characters \\t means tabulation symbol.

You should either prepend your string with @ sign like

@"D:\Temp\textdata.txt"

or use double slashes like

"D:\\Temp\\textdata.txt"
string value = File.ReadAllText(@"D:\temp\textdata.txt");

Console.WriteLine(value);

Note the ' @ ', this is an escape character for the extra back slash in your path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM