DirectoryInfo.Exists always returns false during MSTest

こ雲淡風輕ζ 提交于 2019-12-02 00:48:15

问题


I have a little bit of logic at the boundary of my app dealing with creating directories. I would like to test that it actually creates directories as expected, but the DirectoryInfo.Exists property always returns false even when the directory actually exists.

See also this question - you need to set a breakpoint to see that the directory has actually been created because MSTest will delete it when the test ends.

Is there some setting that tells MSTest to allow "normal" filesystem IO during tests?


回答1:


Assuming you create the DirectoryInfo instance somewhat earlier there is some internal caching of directory state involved - if you call DirectoryInfo.Refresh() to force an update this should work:

var dir = new DirectoryInfo(@".\someDir");
//...other things here
dir.Refresh();
bool doesExist = dir.Exists;


来源:https://stackoverflow.com/questions/8331467/directoryinfo-exists-always-returns-false-during-mstest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!