开发者

How to read files

开发者 https://www.devze.com 2023-02-18 04:56 出处:网络
How to read files in C#? What are the avai开发者_StackOverflowbable methods? Well, there\'s the File class.Vague question, but on the information given and assuming text files:

How to read files in C#?

What are the avai开发者_StackOverflowbable methods?


Well, there's the File class.


Vague question, but on the information given and assuming text files:

string fileData = System.IO.File.ReadAllText(@"C:\path\to\your\file.txt");

However if you are reading binary files, xml files etc there are other ways.


How to: Read From a Text File (C# Programming Guide)


You're looking for every single class in System.IO except for MemoryStream.
For more details, see the documentation.


I always read .txt files with System.IO.StreamReader

StreamReader file = new StreamReader(@"C:\Windows\System32\etc.txt");

And then you can read from the file with

string blah = file.ReadLine();

or

string blahblah = file.Read()


include System.IO; //The input/output class in C# .NET

//Main Class etc.
StreamReader sr = new StreamReader(string Path);
string output = sr.Read(); //output data

C# tends to be very picky with reading data so I suggest reading binary data rather than using StreamReader.


Using System.IO.StreamReader and the .ReadLine() method will work if you need a text file line by line.

0

精彩评论

暂无评论...
验证码 换一张
取 消