C#追加时间戳到文件路径 [英] C# Append Timestamp to Filepath
问题描述
我有以下问题或问题,
我有这个功能
$ p $私人无效SavePic(画布画布,字符串文件名)
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)canvas.Width,(int)canvas.Height,
96d,96d,PixelFormats.Pbgra32);
// //否则需要图像输出为黑色
canvas.Measure(new Size((int)canvas.Width,(int)canvas.Height));
canvas.Arrange(new Rect(new Size((int)canvas.Width,(int)canvas.Height)));
renderBitmap.Render(canvas);
// JpegBitmapEncoder encoder = new JpegBitmapEncoder();
PngBitmapEncoder编码器=新的PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); (FileStream file = File.Create(filename))
{
encoder.Save(file);
code
$ p
$ $ c> SavePic(mySuperDefaultPainting,@C:\KinDraw\out.png);
现在我想附上文件名称的日期+时间?
您可以在函数调用中获取这个DateTime函数吗?
也许我可以在这里找人帮忙吗?
尝试(针对文件路径更新):
string fileName = string.Format({0} - {1:ddMMMyyyy-HHmm} .png,@C:\KinDraw\out,
DateTime.Now);
if(!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}
SavePic(mySuperDefaultPainting,fileName);
假设时间是 29-JAN-2013 07:30 PM 会给你: C:\KinDraw\out-29JAN2013-1930.png
。
有关 CreateDirectory
的详细信息,请访问此MSDN页面。还要查找 Exceptions
,然后换行 try-catch
blocks。
I have the following problem or question, I have this function
private void SavePic(Canvas canvas, string filename)
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)canvas.Width, (int)canvas.Height,
96d, 96d, PixelFormats.Pbgra32);
// needed otherwise the image output is black
canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
renderBitmap.Render(canvas);
//JpegBitmapEncoder encoder = new JpegBitmapEncoder();
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
using (FileStream file = File.Create(filename))
{
encoder.Save(file);
}
}
and the corresponding call
SavePic(mySuperDefaultPainting, @"C:\KinDraw\out.png");
Now I wanted to attach the file name the date + time? You can grab this DateTime function in the function call?
maybe I can someone help here?
try (Updated for File Path):
string fileName=string.Format("{0}-{1:ddMMMyyyy-HHmm}.png", @"C:\KinDraw\out",
DateTime.Now);
if(!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}
SavePic(mySuperDefaultPainting, fileName);
Say the time is 29-JAN-2013 07:30 PM it will give you: C:\KinDraw\out-29JAN2013-1930.png
.
But please check details about CreateDirectory
on this MSDN page. Also look for Exceptions
and wrap in try-catch
blocks.
这篇关于C#追加时间戳到文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!