Search code examples
c#dynamicpathprintscreen

Making a dynamic path string for saving an image


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/


Solution

  • 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"