繁体   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