C# 通过文本框为ArrayList指定的文件路径
我试图允许用户使用通过文本框输入的文本指定用于生成ArrayList的文件的路径,然后用于图表(字符串的格式为@“C:\filename.csv”,该格式以前在硬编码路径时使用并工作)。但是,每次执行代码时,我都会收到一个未处理的参数异常错误。如何着手修复此错误C# 通过文本框为ArrayList指定的文件路径,c#,csv,arraylist,textbox,filepath,C#,Csv,Arraylist,Textbox,Filepath,我试图允许用户使用通过文本框输入的文本指定用于生成ArrayList的文件的路径,然后用于图表(字符串的格式为@“C:\filename.csv”,该格式以前在硬编码路径时使用并工作)。但是,每次执行代码时,我都会收到一个未处理的参数异常错误。如何着手修复此错误 private ArrayList readData() { logFile = textFilePath.Text; // initialise an array list for s
private ArrayList readData()
{
logFile = textFilePath.Text;
// initialise an array list for storing the data
ArrayList logData = new ArrayList();
// read every line in the file
using (StreamReader reader = new StreamReader(logFile))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// split the line into a string array using the comma
// delimiter and add to the array list
string[] parts = line.Split(',');
logData.Add(parts);
}
}
// strip the first element from our array list, as this contains
// the header information from our file
logData.RemoveAt(0);
// return the array list containing the file contents
return logData;
}
有像CSVHelper这样的工具,它将在一行代码中解析CSV,并将结果放入比ArrayList更新的内容中。除非你描述一下异常发生的地方,否则你不可能得到太多帮助。我的错。“mscorlib.dll中发生“System.ArgumentException”类型的未处理异常”是一个精确的错误,它发生在开始StreamReader进程时(以“using”开头的行)。可能需要将其添加到实际问题中。听起来文本框没有有效和/或完整的文件路径。它在文本框中做什么?您可以使用
FileReadAllLines
读入文件并按类似方式处理返回的数组,而不是使用FileOpenDialog键入全名,然后在发布的方法中使用FileReadAllLines
。使用String.Split()解析CSV可能会有很大的问题;因此,有太多的工具来做正确的事情可能会有帮助:问题是VB,但答案没有使用任何特定于VB的方法。它应该会告诉你,像CSVHelper isAh这样的东西我看是多么容易。我将研究FileOpenDialog。看来,在我如此糟糕地提出这个问题之前,我应该做更多的研究。我很抱歉,但谢谢你给我指出了正确的方向。