Making a dynamic path string for saving an image

391 Views Asked by At

First time posting on this forum, and also very new to coding. Sorry if this is an easy question but I'm doing all sorts of things wrong.

The situation I want to save a screenshot, and I want to filename to be the current date like:

string path = DateTime.Now.ToShortDateString().ToString();

And I'm trying this:

ScreenCapture s = new ScreenCapture();
s.CaptureWindowToFile(this.Handle,(@"C:\images\" + path + ".png"), ImageFormat.Png);

which is a mess, and doesn't work. Any help would be appreciated even if you tell me to first learn more and then start with my own projects.

EDIT: The screencapture class I used: http://www.developerfusion.com/code/4630/capture-a-screen-shot/

2

There are 2 best solutions below

1
On BEST ANSWER

You probably have invalid characters in your time string. Use:

DateTime.Now.ToString("ddMMyyHHmmss");

You can change the order as you like. The order I wrote is "day, month, year, hour, minute, second"

4
On

you could declare your variable using the examples below

var path = DateTime.Now.ToString("ddMMyyHHmmss");

or

 string path = DateTime.Now.ToString("ddMMyyHHmmss");