C# 创建文件夹,并在文件夹下创建csv文件
-
创建文件夹
在当前目录下增加文件夹,名称为Spectrum Records+time当前时间,再判断该文件夹是否存在
string time = DateTime.Now.ToString("yyyyMMdd");
string filePath = ".\\Spectrum Records " + time;
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
- 在文件夹下创建csv文件
csv文件名为time.csv
string path = filePath + "\\" + time+ ".csv";
string temp = string.Format("{0},{1}", 111111, 22222);
StreamWriter swrite = new StreamWriter(path, true, Encoding.UTF8);
swrite.Write(temp);
swrite.Write("\r\n");
swrite.Flush();
swrite.Close();
- 注意-前车之鉴
在之前的操作中,发生过“不支持给定路径的格式”的异常,可能因为手敲路径时引起的细微差别而异常,所以最好在创建csv的文件名及路径时,直接引用文件夹的路径变量,即例子中的filePath。