Reputation: 669
I am new to c#, I have made a function which creates a new folder with a time stamp everytime the console is run.
string newfolder = @"d:\Denby_Screenshots" + DateTime.Now.ToString(" yyyy-MM-dd-HH-mm-ss-fff");
if (!Directory.Exists(newfolder))
{
Directory.CreateDirectory(newfolder);
Console.WriteLine("Screenshot folder has been created");
}
I then would like this to be down to allow the screenshots to be saved into this newly created file.
static private Test_Criteria Block_Two(IWebDriver driver, Screenshot screenshot, string newfolder)
{
{
screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile("d:\\ScreenShot.png",System.Drawing.Imaging.ImageFormat.Png);
But for the life of me I am not sure how to do this would anyone be able to advise or have any good screenshots for me to be able to work from. Thanks
Upvotes: 0
Views: 4058
Reputation: 11040
Like so:
screenshot.SaveAsFile(Path.Combine(newFolder,"screenshot.png", ImageFormat.Png);
And reconsider your folder structure, it would be extremely annoying to have a zillion folders in your root folder with just one file in each.
A better approach would be
newFolder = Path.Combine(Environment.SpecialFolder.MyPictures, "Screenshots",DateTime.Now.ToString("yyyyMMdd"));
And the target file name:
Path.Combine(newFolder, "Screenshot "+DateTime.Now.ToString("HH-mm-ss-fff")+".png");
Upvotes: 1
Reputation: 3444
If you are doing these two steps in one console context. Then simply, return newly created folder name to the calling function. And use that folder name (as string) to save files into.
Something like this:
SaveAsFile(Path.Combine(returnedFolderName, suggestedFileName).....
Upvotes: 1