我希望能够从文本文件中读取数字,并将它们存储在字符串数组中,以便在listBox中显示它们。
下面是代码:
string[] filePath = @"C:Site\Project3\Sales.txt";
foreach (string val in filePath)
{
listBoxValues.Items.Add(val.ToString());
}
发布于 2018-07-29 07:49:12
你可以这样做:
public List<string> ToList(string filePath)
{
// Identifiers used are:
var valueList = List<string>();
var fileStream = new StreamReader(filePath);
string line;
// Read the file line by line
while ((line = fileStream.readLine()) != null)
{
// Split the line by the deliminator (the line is a single value)
valueList.Add(line);
}
}
或者你也可以尝试这样的更通用的东西:
public List<string> ToList(string filePath, char deliminator=',')
{
// Identifiers used are:
var valueList = List<string>();
var fileStream = new StreamReader(filePath);
string line;
// Read the file line by line
while ((line = fileStream.readLine()) != null)
{
// Split the line by the deliminator
var splitLine = line.Split(deliminator);
foreach (string value in splitLine)
{
valueList.Add(value);
}
}
}
然后,您可以使用此选项填充列表框。这不是最有效的方法,但它应该适用于您的情况,如果需要,您可以在此基础上进行构建。
https://stackoverflow.com/questions/51576019
复制相似问题