Save Screenshot to folder in root directory VSTS

2019-09-14 05:27发布

I have a simple function that takes screenshots while running Xunit test. The test is straight forward.

 ITakesScreenshot screenshotHandler = PropertiesCollection.driver as ITakesScreenshot;
            Screenshot screenshot = screenshotHandler.GetScreenshot();
            screenshot.SaveAsFile(@"C:\Users\Slim\Screenshots\" + filename + ".png", ImageFormat.Png);

And it works, but we are using VSTS and when somebody else is using the test, it breaks since the path is no longer valid. C:\Users\Slim\Screenshots\ It is possible to change the path of the code to the path where you have the files locally but that is not good practice of course :)

I have tried to use AppDomain.CurrentDomain.BaseDirectory but with no luck.

ITakesScreenshot screenshotHandler = PropertiesCollection.driver as ITakesScreenshot;
            Screenshot screenshot = screenshotHandler.GetScreenshot();
            screenshot.SaveAsFile(AppDomain.CurrentDomain.BaseDirectory + //Screenshots//" + filename + ".png", ImageFormat.Png);

Any good advice's?

1条回答
Emotional °昔
2楼-- · 2019-09-14 06:18

You can take a look at the SpecialFolders enumeration.

It handles the access to paths such as Program Data, Program Files, App Data, etc. And the users accounts are correctly handled if you use a folder that is user specific, such as LocalApplicationData or RoamingApplicationData, which respectively map to C:\users\MyUser\AppData\Local and C:\users\MyUser\AppData\Local\Roaming.

On the opposite side, you have for instance CommonApplicationData which usually maps to C:\ProgramData, which is common for all users.

So, to sum up, you could do something like:

string myFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
screenshot.SaveAsFile(Path.Combine(myFolder, filename + ".png"), ImageFormat.Png);
查看更多
登录 后发表回答