1. 程式人生 > 其它 >[c#] 用日期做檔名報錯 System.IO.IOException

[c#] 用日期做檔名報錯 System.IO.IOException

想要用日期做檔名結果一直報錯,

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Text;
 5 
 6 namespace FileCSVDemo
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string path = @"..\..\..\2021年11月05日14:12:02.csv";
13             //
" + DateTime.Now.ToString("yyyy年MM月dd日HH:mm:ss") + " //一開始是用這種方式寫的為了找問題直接輸入時期格式 14 if (!File.Exists(path)) 15 { 16 17 File.Create(path).Close(); 18 } 19 20 StreamWriter sw = new StreamWriter(path, 21                     true
,Encoding.UTF8); 22 23 for (int i = 0; i < 3; i++) 24 { 25 sw.Write("阿斯頓" + i + ","); 26 sw.Write("的撒的" + i + ","); 27 sw.Write("他們" + "\r\n"); 28 29 } 30 31 sw.Flush(); 32 sw.Close(); 33 34
} 35 } 36 }

後來發現是冒號的問題, 檔名中不能出現冒號.

於是便全部用漢字代替了

1 string path = @"..\..\..\2021年11月05日14時12分02.csv";
2             //" + DateTime.Now.ToString("yyyy年MM月dd日HH時mm分ss") + "

才疏學淺, 見笑了