0

我是 c# 的新手,我创建了一个函数,每次运行控制台时都会创建一个带有时间戳的新文件夹。

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");
        }

然后,我希望将其关闭以允许将屏幕截图保存到这个新创建的文件中。

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

但是对于我的生活,我不确定如何做到这一点,任何人都可以提供建议或有任何好的屏幕截图供我使用。谢谢

4

2 回答 2

1

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");
于 2013-03-12T14:20:48.397 回答
1

如果您在一个控制台上下文中执行这两个步骤。然后简单地将新创建的文件夹名称返回给调用函数。并使用该文件夹名称(作为字符串)将文件保存到其中。

像这样的东西:

SaveAsFile(Path.Combine(returnedFolderName, suggestedFileName).....

于 2013-03-12T14:13:48.650 回答