[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") + "
才疏学浅, 见笑了
浙公网安备 33010602011771号