怎么可能用多行读取文本文件,然后把文本文件中的每一行放在ListBox的一个单独的行中呢?
我到目前为止的代码是:
richTextBox5.Text = File.ReadAllText("ignore.txt");
在低于10的Windows版本上安装ClickOnce应用程序时出错
http.sys&响应时间logging
.NET字典键作为列表框数据源
用C#挑选某个数组的string
哪个.NET框架版本将被包含在Windows 7中?
计算Windows进程的cpu使用率?
selectListView的特定列并将其打印在C#.net的新消息框中
如何使我的服务停止自动发布到Windows应用程序事件日志
String text = File.ReadAllText("ignore.txt"); var result = Regex.Split(text,"rn|r|n"); foreach(string s in result) { lstBox.Items.Add(s); }
string[] lines = System.IO.File.ReadAllLines(@"ignore.txt"); foreach (string line in lines) { listBox.Items.Add(line); }
static IEnumerable<string> ReadFromFile(string file) {// check if file exist,null or empty string string line; using(var reader = File.OpenText(file)) { while((line = reader.ReadLine()) != null) { yield return line; } } }
用它
var lines = ReadFromFile(myfile); myListBox.ItemsSource = lines.ToList(); // or change it to ObservableCollection. also you can add to the end line by line with myListBox.Items.Add()
您应该使用流读取器一次读取一行文件。
using (StreamReader sr = new StreamReader("ignore.txt")) { string line; while ((line = sr.ReadLine()) != null) listBox1.Items.Add(line); }
StreamReader信息 – > http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
列表框信息 – > http://msdn.microsoft.com/en-us/library/system.windows.forms.listBox.aspx
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。