viv1d
viv1d

Reputation: 349

C# Append Timestamp to Filepath

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?

Upvotes: 0

Views: 12289

Answers (5)

Jabir
Jabir

Reputation: 81

This is how I did it and it works. Tweaked @Avishek code a bit to make it work for mine. No need to delete file or lose what's in it.

Call "Rename()" method after you output the file..

public static void Rename()
        {
            string timestamp = DateTime.Now.ToString("MMddyyyy.HHmmss"); 
            string originalFile = @"C:\Users\Data_Output\" + fileName + ".csv"; 
            string newFile = @"C:\Users\Data_Output\" + fileName + "_" + timestamp + ".csv";

            File.Move(originalFile, newFile);

        }

Upvotes: -1

TheVillageIdiot
TheVillageIdiot

Reputation: 40537

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.

Upvotes: 3

Avishek
Avishek

Reputation: 1896

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 :)

Upvotes: 2

Roy Dictus
Roy Dictus

Reputation: 33149

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.

Upvotes: 1

nonacc
nonacc

Reputation: 21

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;

Upvotes: 1

Related Questions