C# Append Timestamp to Filepath

让人想犯罪 __ 提交于 2020-01-07 05:01:29

问题


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?


回答1:


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.




回答2:


Try to add this at the beginning of your code:

var extension = Path.GetExtension(filename);
var newName = filename.Replace(filename, extension) + DateTime.Now.ToString("yyyy-MM-dd HH:mm:dd") + extension;



回答3:


Just put this line in there:

string stampedFileName = filename.Replace(".",
    string.Format("{0:YYYY-mm-dd hhmmss}", DateTime.UtcNow) + ".");

and then change

using (FileStream file = File.Create(filename))

to

using (FileStream file = File.Create(stampedFilename))

It is important to use DateTime.UtcNow rather than DateTime.Now because the former is not influenced by daylight saving time.

EDIT: The format I propose above has the advantage that sorting your filenames alphabetically then automatically also sorts them chronologically.




回答4:


string timestamp =DateTime.Now.ToString("MMddyyyy.HHmmss");
SavePic(mySuperDefaultPainting, @"C:\KinDraw\out"+timestamp+".png");

Update: (to create the directory if it does not exist)

if (!Directory.Exists(filepath))
    Directory.CreateDirectory(filepath);

Hope it helps :)




回答5:


Usage:

string result = "myfile.txt".AppendTimeStamp();
//myfile20130604234625642.txt

Extension method

public static class MyExtensions
{
    public static string AppendTimeStamp(this string fileName)
    {
        return string.Concat(
            Path.GetFileNameWithoutExtension(fileName),
            DateTime.Now.ToString("yyyyMMddHHmmssfff"),
            Path.GetExtension(fileName)
            );
    }
}


来源:https://stackoverflow.com/questions/14578592/c-sharp-append-timestamp-to-filepath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!