iT邦幫忙

0

C#讀csv再寫入相同的內容卻空白了??

  • 分享至 

  • xImage

請問一下..為何我list保存再for裡面看的到..再寫進去..去開該csv卻是空白檔??

private void button1_Click(object sender, EventArgs e)
        {
            DateTime CurrTime = DateTime.Now;
            string year = CurrTime.Year.ToString();
            string month = CurrTime.Date.Month.ToString();
            string day = CurrTime.Date.Day.ToString();
            string path = "D:\\CVP_System\\"+year+"_"+month+".csv";
            ArrayList list = new ArrayList();
            string[] s1 = new string[31];
            string[] s2 = new string[3];
            string[] s3 = new string[2];
            int i = 0;
            if (File.Exists(path))
            {
                sr = new StreamReader(path);
                while (!sr.EndOfStream)
                {
                    list.Add(sr.ReadLine());
                }
                sr.Close();
                int x=list.Count;
                s2 = list[x-1].ToString().Split('_');
                s3=s2[1].Split(',');
                //MessageBox.Show(s3[0].Equals(day).ToString());
                if (s3[0] == day) 
                {
                    StreamWriter sw = new StreamWriter(path);
                    for(int z = 0; z < x; z++) 
                    {
                        sw.WriteLine(list[z].ToString());
                    }
                }
            }
        }

圖片
https://ithelp.ithome.com.tw/upload/images/20220416/20141745cw02MPhdJz.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
海綿寶寶
iT邦大神 1 級 ‧ 2022-04-16 15:33:29
最佳解答
if (s3[0] == day) 
{
    StreamWriter sw = new StreamWriter(path);
    for(int z = 0; z < x; z++) 
    {
        sw.WriteLine(list[z].ToString());
    }
}

加一列試試看

if (s3[0] == day) 
{
    StreamWriter sw = new StreamWriter(path);
    for(int z = 0; z < x; z++) 
    {
        sw.WriteLine(list[z].ToString());
    }
    sw.close();
}
rabbit iT邦新手 4 級 ‧ 2022-04-16 22:11:42 檢舉

真的非常感謝海綿寶寶..從andorid到C#..因為現實原因就沒走android也就上了C#的路..這邊海綿還是能解..只能說海綿寶寶太神了..感謝感謝

0
Yaowen
iT邦研究生 4 級 ‧ 2022-04-16 18:02:01

可以使用 CsvHelper 套件
參考如下

我要發表回答

立即登入回答