Lm th? no ? ?c m?t t?p tin vn b?n b?ng cch s? d?ng System.IO v Visual C#

D?ch tiu ? D?ch tiu ?
ID c?a bi: 306777 - Xem s?n ph?m m bi ny p d?ng vo.
?i v?i m?t Microsoft Visual Basic.NET Phin b?n c?a bi vi?t ny, xem 302309.

Bi vi?t ny ? c?p ?n Microsoft sau.Khng gian tn th vi?n l?p c?a NET Framework:
  • System.io
  • System.collections
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t t?ng b?c ny cho b?n th?y lm th? no ? l?y l?i thng tin t? t?p vn b?n (.txt), v sau s? d?ng m?t ArrayList l?p h?c ? hi?n th? thng tin cho ng?i dng.

Yu c?u

Microsoft Visual C#

?c t?p tin vn b?n trong Visual C#

M? v ?c t?p tin ? ?c truy c?p l m?t ph?n quan tr?ng c?a ?u vo/?u ra (IO) ch?c nng, ngay c? khi b?n khng c?n ph?i vi?t th cho cc t?p tin trong cu h?i. V d? ny s? m? ra m?t t?p tin ? ?c, l h?u ch cho vi?c ?c t?p tin vn b?n, nhng khng thch h?p cho t?p tin nh? phn. V d? ny s? d?ng m?t trong nhi?u phng php m c s?n cho vi?c m? t?p tin.

M?c d nhi?u c?u trc d? li?u c s?n ? lu tr? thng tin ?c l?y t? cc t?p tin, m?t ArrayList l?p l c?u trc n gi?n nh?t ? s? d?ng. ? m? v ?c t? t?p, v d? ny s? d?ng cc ?i t?ng t? cc System.io khng gian tn, c? th? cc System.io.StreamReader l?p h?c.

Chu y M?u ny ?i h?i m?t s? h?nh th?c c?a m?t t?p tin vn b?n (.txt) m t? ? ?c.

? t?i v ?c qua m?t t?p tin vn b?n t? Visual C#, h?y lm theo cc b?c sau:
  1. B?t ?u Microsoft Visual Studio.
  2. T?o m?t ?ng d?ng giao di?n i?u khi?n m?i trong Visual C#. Visual Studio t?o ra m?t L?p h?c t?nh ?i v?i b?n, cng v?i m?t s?n ph?m no Main() th? t?c.
  3. Ki?m ch?ng r?ng d? n tham chi?u t cc Cac vn khng gian tn. S? d?ng cc b?ng cch s? d?ng tuyn b? v? s? Cac vn , System.io, v System.collections khng gian tn do b?n khng ph?i v?t qua v?ng lo?i t? khai t? cc khng gian tn sau trong m? c?a b?n. B?n ph?i s? d?ng cc bo co ny tr?c khi b?t k? t? khai khc.
    using System;
    using System.IO;
    using System.Collections;
    					
  4. ? m? m?t t?p tin cho ?c sch, t?o ra m?t tr?ng h?p m?i c?a m?t StreamReader ?i t?ng, v v?t qua con ?ng c?a t?p tin vo cc nh xy d?ng nh sau:
    StreamReader objReader = new StreamReader("c:\\test.txt");
    					
  5. B?n ph?i c m?t bi?n chu?i trong ? lu tr? m?i d?ng c?a t?p tin khi b?n x? l?. B?i v? b?n s? c thm nh?ng d?ng ny ?n m?t ArrayList, tuyn b? v t?o m?t ?i t?ng c?a lo?i, c?ng.
    string sLine="";
    ArrayList arrText = new ArrayList();
    					
  6. C m?t s? cch ? ?c t?p tin, bao g?m c? cc ReadToEnd phng php m ?c trong t?p tin ton b? cng m?t lc. Tuy nhin, v d? ny, b?n c th? s? d?ng cc ReadLine phng php ? mang l?i trong t?p tin m?t d?ng m?t lc. Khi k?t thc t?p ?c ?t t?i, phng php ny tr? v? m?t null, cung c?p m?t cch ? b?n c th? k?t thc v?ng l?p c?a b?n. Khi b?n ?c m?i d?ng t? t?p tin, b?n c th? s? d?ng cc Thm phng php c?a cc ArrayList ? chn cc ?ng vo c?a b?n ArrayList l?p h?c.
    while (sLine != null)
    {
    	sLine = objReader.ReadLine();
    	if (sLine != null)
    		arrText.Add(sLine);
    }
    objReader.Close();
    					
  7. S? d?ng m?t ?i v?i m?i v?ng l?p ? vi?t n?i dung c?a b?n v?a ?c ?y ArrayList giao di?n i?u khi?n nh sau:
    foreach (string sOutput in arrText)
    	Console.WriteLine(sOutput);
    Console.ReadLine();
    					
  8. Lu v ch?y m? c?a b?n, trong s?n xu?t m?t danh sch cc t?p tin c?a b?n trn bn i?u khi?n.

Danh sch m? hon ch?nh

using System;
using System.IO;
using System.Collections;

namespace TextFileReader_csharp
{
	/// <summary>
	/// 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();
		}
	}
}
				

Khc phuc s c

C r?t nhi?u i?u c?n bi?t khi b?n lm vi?c v?i t?p i/O, bao g?m cc m?c sau y:
  • B?t c? lc no b?n truy c?p vo m?t t?p tin, khng c kh? nng r?ng cc t?p tin m b?n ang c? g?ng ? ?c ho?c vi?t c th? khng trn h? th?ng ho?c s? d?ng.
  • V d? ny l?n ?c t?p tin ton b? vo b? nh? tr?c khi n x? l? cc t?p tin. B?n c th? g?p ph?i m?t t?nh hu?ng m trong cc t?p tin l qu l?n ? ?c t? ch?c trong b? nh?, ho?c b?n c th? khng c quy?n truy c?p vo cc t?p tin.
B?t k? c?a cc t?nh hu?ng ny gy ra m?t ngo?i l? ? ?c nng ln. N lun lun l th?c hnh t?t ? cung c?p m?t H?y th?... b?t kh?i ? x? l? nh?ng v?n ? ph? bi?n.

Thu?c tnh

ID c?a bi: 306777 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2008 Express Edition
T? kha:
kbcollections kbhowtomaster kbio kbmt KB306777 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:306777

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com