using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; //********************************************************* // Simple file I/O example:: Get data from a file, the // file should pre-exist and by in your project/bin/Release // folder. Edwin Armstrong A.K.A. (efa) //********************************************************* // Date Comment who //========= ======================================= === // 02/22/10 Working and tested version. efa //********************************************************* namespace file_io1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str1 = "nouns.txt"; string[] nouns = new string[100]; StreamReader sr; Random random = new Random(); try // open text file for read { sr = new StreamReader(str1); } catch { MessageBox.Show("Can't find file [" + str1 + "]"); return; } int c = 0; while( (nouns[c] = sr.ReadLine()) != null) { ++c; if (c >= 100)// just for safety break; } int sel = 0; sel = random.Next(c); textBox1.Text = nouns[sel]; // Close StreamReader sr.Close(); } } }