我有一個問題,我相信它很簡單,但它不工作在我的代碼。我正在創建一個日誌文件,我想將日期連接到文件名,以便他們知道何時生成日誌。我的string.replace不會從我的日期中刪除「\」,我已經轉換爲字符串。String.replace消除反斜槓
見下面的代碼:
string DateNow = Convert.ToString(System.DateTime.Now);
DateNow = DateNow.Substring(0, 10);
DateNow.Replace(@"\\", "-");
string FileName = "log" + DateNow + ".txt";
// Write values to textfile and save to Log folder
using (StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath("~/Log" + FileName)))
{
sw.WriteLine(System.DateTime.Now);
sw.WriteLine("New user created");
sw.WriteLine("Username is: " + username);
sw.WriteLine("Password is: " + password);
sw.WriteLine("Company is: " + company);
sw.WriteLine("Email is: " + email);
sw.Dispose();
sw.Close();
}
這將拋出一個異常,因爲在Windows文件名不能包含\字符。任何想法爲什麼替換方法不起作用?
謝謝。
System.DateTime.Now.ToString('MM-dd-yyyy'); – Sender