簡體   English   中英

將日期添加到文件名時,StreamWriter問題

[英]c# - StreamWriter Issue when adding Date to the filename

我在使用System.IO.StreamWriter遇到了一個小問題。

StreamWriter Write = 
   new StreamWriter(@"../Debug/Payments/_" + dp.Value.ToString() + ".txt");

當我運行該應用程序時,它返回:

mscorlib.dll中發生了類型為'System.NotSupportedException'的未處理異常。其他信息:不支持給定路徑的格式。

嘗試這個:

string filename= DateTime.Now.ToString("yyyy_MM_dd");
string strpath = Server.MapPath("~/Debug/Payments/_"+ filename + ".txt");
StreamWriter Write = new StreamWriter(strpath);

您的問題是格式。

由於您說dp是日期選擇器,因此ToString()幾乎肯定會在文件名中插入諸如:之類的無效字符,例如

Payments/_2017/11/13 1:14:13 PM.txt

另外,根據注釋,您需要更改斜線方向。

您需要使用更安全的日期格式,如下所示:

$@"..\Debug\Payments\_{dp.Value:yyyy-MM-dd}.txt"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM