使用当前日期和时间为文件名保存文件 Winform C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15267168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving a file with current date & time for the file name Winform C#
提问by N0xus
I've made it possible to save a file from my winform using the Vlcdotnet framework. At the moment this code is as follows:
我已经可以使用 Vlcdotnet 框架从我的 winform 中保存文件。目前这段代码如下:
_tempVLCWindow.TakeSnapshot("C:\ScreenCap.jpg", 1280, 720);
Now, instead of "ScreenCap" being the file (which only allows me to save one single image) I want it to store the current date & time so I can save multiple snapshots. How is this possible?
现在,不是“ScreenCap”作为文件(它只允许我保存一张图像),我希望它存储当前日期和时间,以便我可以保存多个快照。这怎么可能?
采纳答案by ceth
you can do following:
您可以执行以下操作:
String fileName = "C:\ScreenCap_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".jpg";
_tempVLCWindow.TakeSnapshot(fileName , 1280, 720);
This will create you a filename like: C:\ScreenCap_20130307_1023.jpg
这将为您创建一个文件名,如:C:\ScreenCap_20130307_1023.jpg
By calling ToString() and specifying the format as YearMonthDay_HourMinutesSeconds (yyyyMMdd_hhmmss) you will be able to create a string with the date and time that will be accepted as a file name. If you did call only .ToString() you will get an illegal characters in path exception.
通过调用 ToString() 并将格式指定为 YearMonthDay_HourMinutesSeconds (yyyyMMdd_hhmmss),您将能够创建一个包含将被接受为文件名的日期和时间的字符串。如果你只调用 .ToString() 你会在路径异常中得到一个非法字符。
For formatting options in date.tostring("")look at: msdn
有关date.tostring("") 中的格式化选项,请查看:msdn
回答by zkanoca
If I did not get you wrong you may try this:
如果我没有误会你,你可以试试这个:
string filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString();
_tempVLCWindow.TakeSnapshot(String.Format("C:\{0}.jpg", filename), 1280, 720);