所以我嘗試閱讀該檔案并確認它有效,然后嘗試將內容添加到串列框中,但它不起作用。我這樣做的方法是首先讀取檔案,逐行拆分檔案并將內容放入字串陣列,然后使用 for 回圈將內容添加到串列框。起初我的問題是串列框沒有添加內容,然后發生了一些變化,現在看起來檔案沒有被正確讀取。我不知道它是否會影響任何東西,但我確實在 Visual Studio 中編輯了檔案,所以我認為這不會影響任何東西,但我提一下以防萬一。由于我正在閱讀多個檔案,因此我也不止一次使用 holdLine。
這是我正在閱讀檔案的地方
using (StreamReader inOFile = new StreamReader("..\\..\\options.txt"))
{
while (!inOFile.EndOfStream)
{
holdLine = inOFile.ReadLine();
OptionsArr = holdLine.Split('\n');
}
有問題的檔案有這些行
- 員工ID
- 姓名
- 部
- 就業狀況
- 薪水
我嘗試將字串陣列的內容添加到串列框的代碼
OptionsListBox.Items.Clear();
OptionsListBox.BeginUpdate();
//one way I tried
string listOptions = string.Join(" ", Program.OptionsArr);
OptionsListBox.Items.Add(listOptions.ToString());
//different way i tried
for (int i = 0; i < Program.OptionsArr.Length; i )
{
OptionsListBox.Items.Add(Program.OptionsArr[i]);
}
//another failed attempt
OptionsListBox.Items.AddRange(Program.OptionsArr);
OptionsListBox.EndUpdate();
uj5u.com熱心網友回復:
這是一些演示問題的代碼。我添加了一些 Console.WriteLine() 來列印回圈期間和之后變數中的內容。(這是一個小提琴)
using System;
using System.IO;
public class Program
{
const string _textFile = "This is the first line in the file\n"
"This is the second line in the file\n"
"This is the third line in the file\n";
public static void Main()
{
var utf8 = System.Text.Encoding.UTF8.GetBytes(_textFile);
var memoryStream = new MemoryStream(utf8);
memoryStream.Position = 0;
string wholeLine;
string[] optionsArray;
using (var inOFile = new StreamReader(memoryStream))
{
var count = 0;
while (!inOFile.EndOfStream)
{
wholeLine = inOFile.ReadLine();
optionsArray = wholeLine.Split('\n');
Console.WriteLine("Reading Line {0}", count);
Console.WriteLine("\tWhole: '{0}'", wholeLine);
Console.WriteLine("\tSplit: '{0}'", string.Join("', '", optionsArray));
Console.WriteLine()
}
Console.WriteLine("Final Options Array: {0}", string.Join(" | ", optionsArray));
}
}
}
這個程式的輸出是:
Reading Line 1
Whole: 'This is the first line in the file'
Split: 'This is the first line in the file'
Reading Line 2
Whole: 'This is the second line in the file'
Split: 'This is the second line in the file'
Reading Line 3
Whole: 'This is the third line in the file'
Split: 'This is the third line in the file'
Final Options Array: This is the third line in the file
注意 optionsArray 中只包含一項嗎?它是 WholeLine 的精確副本?這是因為 ReadLine() 函式洗掉了資料中的所有換行符。.Split('\n') 將無法拆分任何內容。
如果我將拆分字符更改為空格,則會得到以下資訊:
Reading Line 1
Whole: 'This is the first line in the file'
Split: 'This', 'is', 'the', 'first', 'line', 'in', 'the', 'file'
Reading Line 2
Whole: 'This is the second line in the file'
Split: 'This', 'is', 'the', 'second', 'line', 'in', 'the', 'file'
Reading Line 3
Whole: 'This is the third line in the file'
Split: 'This', 'is', 'the', 'third', 'line', 'in', 'the', 'file'
Final Options Array: This | is | the | third | line | in | the | file
在這種情況下,每一行都被分成單獨的單詞,因為它們之間用空格“ ”分隔。但即使我更改了拆分,optionsArray 也只包含檔案中的最后一行。您的 while 回圈是為了讀取整個檔案而撰寫的,但是由于您從不做任何事情來收集拆分操作的結果,因此您的其余代碼將不會做任何事情。
uj5u.com熱心網友回復:
嘗試類似:
// Employee is a class with employee fields (EmployeeID, name...)
IList<Employee> employees = readEmployeesFromFile();
OptionsListBox.DataSource = employees;
OptionsListBox.DisplayMember = "Name";
OptionsListBox.ValueMember = "EmployeeID";
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/456504.html
