我在創建文件時出現錯誤,而文件名中當前時間使用了字符串變量。錯誤的以C#創建文件時出現錯誤
「System.NotSupportedException」類型的未處理的異常在mscorlib.dll
其他信息發生 :不支持給定的路徑的格式。
我找不到代碼中出現了什麼問題,我嘗試了沒有「路徑」變量,它會成功運行。當我使用「cTime」變量創建動態文件名時,我不明白髮生了什麼。
static void Main(string[] args)
{
string path = @"C:\\Reminder_Logs\\";
string cTime = DateTime.Now.ToString("HH:mm").Trim();
using (StreamWriter srRun = File.AppendText(path + "log_Reminder_" + cTime + ".txt"))
{
using (StreamWriter sr = File.AppendText(path + "log_Start.txt"))
{
sr.WriteLine("reminder file created!!! " + DateTime.Now.ToString("HH:mm"));
}
}
}
不能使用冒號路徑 –
如果使用@ – active92
你的路徑應該是這樣你只需要一個斜槓:@「C:\ Reminder_Logs \」 –