极度简易的抽奖、随机选择小工具
//开始按钮 private void btnCheckFiles_Click(object sender, EventArgs e) { this.timer1.Enabled = true; //定时器开始运行 this.timer1.Interval = 200; //定时器每次运行间隔时间 } //解析文件,存入List中 int i = 0; public string returnName() { //string haarXmlPath = @"../../haarcascade_frontalface_alt_tree.txt"; FileInfo file = new FileInfo("菜单.txt"); //获取Debug文件目录下的该文件 List<string> list = ReadTextFileToList(file.Name); //List<string> list = ReadTextFileToList(@"F:\2.txt"); 绝对路径,如果该磁盘有限制将拒绝被访问 string name = null; if (i < list.Count) { name = list[i].ToString(); i++; } else { i = 0; } return name; } //文件解析 public List<string> ReadTextFileToList(string fileName) { try { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); //如果未在Debug目录下找到目标文件,将会报错 List<string> list = new List<string>(); StreamReader sr = new StreamReader(fs); //读取文件中字符 sr.BaseStream.Seek(0, SeekOrigin.Begin); string tmp = sr.ReadLine(); while (tmp != null) { list.Add(tmp); tmp = sr.ReadLine(); } sr.Close(); fs.Close(); //释放资源,如果不关闭,此事资源被占用,第二次读取时会报错 return list; } catch (Exception ex) { throw ex; } } //定时器 private void timer1_Tick(object sender, EventArgs e) { lbText.Text = returnName(); } //停止按钮 private void btnStop_Click(object sender, EventArgs e) { this.timer1.Stop(); // this.txtStopName.Text = returnName(); //lbText.Text = returnName(); }