问题描述
我有一个简单的功能,可以在运行Xunit测试时截取屏幕截图.测试很简单.
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);
它可以工作,但是我们正在使用VSTS,而当其他人正在使用测试时,由于路径不再有效,它会中断. C:\ Users \ Slim \ Screenshots \
可以将代码的路径更改为本地拥有文件的路径,但这当然不是一种好习惯:)
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 :)
我尝试使用AppDomain.CurrentDomain.BaseDirectory,但是没有运气.
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);
有什么好的建议吗?
推荐答案
您可以查看 SpecialFolders枚举.
它处理对程序数据,程序文件,应用程序数据等路径的访问.如果使用特定于用户的文件夹(例如LocalApplicationData或RoamingApplicationData,分别映射到 C:\ users \ MyUser \ AppData \ Local 和 C:\ users \ MyUser \ AppData \ Local \ Roaming .
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
.
相反,例如,您有CommonApplicationData,它通常映射到所有用户都通用的 C:\ ProgramData
.
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);
这篇关于将屏幕快照保存到根目录VSTS中的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!