The release number of this article has been CHS306777
This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.
For this Beta product, Microsoft does not provide formal product support. For information on getting beta support, see the documentation included in the beta product file, or view the site you download this version.
For Microsoft Visual Basic .NET versions of this article, see
CHS302309.
This task content
Summary
Requirements to read text files in Visual C # .NET Troubleshooting
Summary This article demonstrates how to retrieve information from text files and use
The ArrayList class displays the information to the user.
Back to top
Request Microsoft Visual C # .NET
Back to top
Reading text files in Visual C # .NET to open and read files to read access is a very important part of the input / output (IO) feature, even if you don't need to write to related files. This example opens a file for reading, which is suitable for reading a text file, but does not apply to read binaries. This example uses a plurality of methods that can be used to open files. Although many data structures can be used to store information retrieved from the file, however,
The ArrayList class is the simplest structure. In order to open the file and read from the file, this example is used.
System.io namespace objects, especially
System.io.streamReader class.
Note: This example requires some form of text (.txt) file to be read from it.
To load and read text files in Visual C # .NET, follow these steps:
Open Visual Studio .NET. Create a console application in the C #. Visual Studio creates a static class and an empty main () process. Make sure the project references at least the System namespace. Using the USING statement for System, System.IO, and System.Collections, so that there is no need to define declarations in these namespaces in the later code. These statements must be before all other declarations. Using system;
Using system.io;
Using system.collections; To open a file for reading, create a new instance of the StreamReader object and pass the path of the file into the constructor (as shown below): streamreader objreader = New StreamReader ("C: //test.txt "); you need a string variable to store each row of the file to this variable when processing. Since these rows will be added to an ArrayList, you should also declare and create an object of this type. String sline = "";
ArrayList Arrtext = New ArrayList (); there are many ways to read this file, including the ReadToEnd method for reading the entire file at once. However, in this example, you can use the Readline method to read only one line in the file each time. When the end of the file is reached, this method returns null value, which can be used to end the loop. When you read each line from a file, you can insert these rows into the ArrayList class using the ARRAYLIST ADD. While (sline! = NULL)
{
Sline = Objreader.Readline ();
IF (sline! = null)
Arrtext.Add (sline);
ObjReader.close (); written to the console using the for Each loop (as shown below): Foreach (String Soutput in Arrtext)
Console.writeLine (Soutput);
Console.readLine (); Save and run your code, which will give the console to generate a list of contents.
Back to top
Complete code list
Using system;
Using system.io;
Using system.collections;
Namespace TextFileReader_csharp
{
///
/// summary description for class1.
/// summary>
Class class1
{
Static void main (string [] args)
{
StreamReader Objreader = New StreamReader ("C: //Test.txt");
String sline = "";
ArrayList Arrtext = New ArrayList ();
While (sline! = NULL)
{
Sline = Objreader.Readline ();
IF (sline! = null)
Arrtext.Add (sline);
}
Objreader.close ();
FOREACH (String Soutput in Arrtext)
Console.writeLine (Soutput);
Console.readline ();
}
}
}
Back to top
Troubleshooting Pay attention to some issues when processing file I / O, including the following:
Whenever you access a file, the file to be read or written may not be on the system, or in use. Before processing the file, this example first reads the entire file to memory. You may encounter the file too large to store the memory, or there is no permission to access the file. Anything above will trigger an exception. It is best to always provide one
Try ... Catch block handles these common problems.
Back to top
Refer to more information, please visit the following Microsoft .NET SDK Quick Start Tutorial Web Site:
http://www.gotdotnet.com/quickstart
Back to top
The information in this article applies to:
Microsoft Visual C # .NET Beta 2
Recent Update: 2001-11-2 (1.0) Keyword Kbhowto KbhowTomaster Kbnokeyword KB306777 KBAUDDEVELOPER