2017-06-02 55 views
0

我在創建文件時出現錯誤,而文件名中當前時間使用了字符串變量。錯誤的以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")); 
       } 
      } 

     } 
+2

不能使用冒號路徑 –

+2

如果使用@ – active92

+2

你的路徑應該是這樣你只需要一個斜槓:@「C:\ Reminder_Logs \」 –

回答

1

在你的代碼的問題是,路徑包含:你可以試試這個

string cTime = DateTime.Now.ToString("HH.mm").Trim(); 
2

:字符無法在文件名中使用,請用

string cTime = DateTime.Now.ToString("HHmm").Trim();例如代替。

1

:字符未在文件路徑接受 path.getinvalidfilenamechars()給出的文件路徑都無效字符