小编jes*_*ice的帖子

将.csv文件加载到字典中,我不断收到错误"无法从'string []'转换为'string'"

我已经使用streamreader读取.csv文件,然后我需要拆分值并将它们放入字典中.到目前为止我有:

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Dictionary<string, string> dict = new Dictionary<string, string>();   
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        using (StreamReader reader = new StreamReader("textwords0.csv"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                string[] parts = line.Split(',');
                dict.Add(parts[0], parts[1]);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我不断收到错误"无法从'string []'转换为'string'",但无法解决如何修复它.

提前致谢!

更新:...我不小心将csv文件保持打开状态并且现在正在工作,抱歉浪费时间我们认为我有一个不同的电子表格打开,一些非常有用的建议,但感谢所有的帮助!

c# dictionary streamreader

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

dictionary ×1

streamreader ×1