StreamReader









How do I use Stream Reader
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: frmThi.cs Copy
26         private void dapan()
27         {
28             StreamReader srdapan = new StreamReader("DAPAN.txt");
29             string line;
30             for (int i = 0; i <= int.Parse(frmluachon.strsoluongcauhoi); i++)
31             {
32                 strdapan[i] = (line = srdapan.ReadLine());
33             }
34         }
File name: frmThi.cs Copy
168         /// Hàm đọc file txt gán dữ liệu vào List(cái này giống Array)
172         private List readfile(string path)
173         {
174             List lst = new List();
175             StreamReader stReader = new StreamReader(path);
176             //Lập lại cho đến câu cuối cùng của file txt
177             while (stReader.Peek() >= 0)
178             {
179                 //Thêm giá trị vào List
180                 lst.Add(stReader.ReadLine());
181             }
182             return lst;
183         }
File name: AccountService.cs Copy
59     public void RegisterByEmail(string email, Origin origin)
60     {
61         this.registrationCallback = null;
62         this.AppId = string.Empty;
63         this.Message = string.Empty;
64         this.ReturnCode = -1;
65
66         string result;
67         try
68         {
69             WebRequest req = HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin));
70             HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
71
72             // now read result
73             StreamReader reader = new StreamReader(resp.GetResponseStream());
74             result = reader.ReadToEnd();
75         }
76         catch (Exception ex)
77         {
78             this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
79             this.Exception = ex;
80             return;
81         }
82
83         this.ParseResult(result);
84     }
File name: AccountService.cs Copy
121     private void OnRegisterByEmailCompleted(IAsyncResult ar)
122     {
123         try
124         {
125             HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
126             HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
127
128             if (response != null && response.StatusCode == HttpStatusCode.OK)
129             {
130                 // no error. use the result
131                 StreamReader reader = new StreamReader(response.GetResponseStream());
132                 string result = reader.ReadToEnd();
133
134                 this.ParseResult(result);
135             }
136             else
137             {
138                 // a response but some error on server. show message
139                 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
140             }
141         }
142         catch (Exception ex)
143         {
144             // not even a response. show message
145             this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
146             this.Exception = ex;
147         }
148
149         if (this.registrationCallback != null)
150         {
151             this.registrationCallback(this);
152         }
153     }
File name: ChatBot.cs Copy
20         private void Form1_Load(object sender, EventArgs e)
21         {
22             bot = new ChatBot();
23
24             // Sets Position for the first bubble on the top
25             bbl_old.Top = 0 - bbl_old.Height;
26
27             // Load Chat from the log file
28             if (File.Exists("chat.log"))
29             {
30                 using (StreamReader sr = File.OpenText("chat.log"))
31                 {
32                     int i = 0; // to count lines
33                     while (sr.Peek() >= 0) // loop till the file ends
34                     {
35                         if (i % 2 == 0) // check if line is even
36                         {
37                             addInMessage(sr.ReadLine());
38                         }
39                         else
40                         {
41                             addOutMessage(sr.ReadLine());
42                         }
43                         i++;
44                     }
45                     // scroll to the bottom once finished loading.
46                     panel2.VerticalScroll.Value = panel2.VerticalScroll.Maximum;
47                     panel2.PerformLayout();
48                 }
49             }
50         }

StreamReader 143 lượt xem

Gõ tìm kiếm nhanh...