c# – 如何修复错误:“当前线程需要将其公寓状态设置为ApartmentState.sta才能启动Internet Explorer”?

前端之家收集整理的这篇文章主要介绍了c# – 如何修复错误:“当前线程需要将其公寓状态设置为ApartmentState.sta才能启动Internet Explorer”?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在C#中有以下代码
  1. namespace Tests
  2. {
  3. [SetUpFixture,RequiresSTA]
  4. public class Setup
  5. {
  6. public IE Window = new IE("webpage");
  7.  
  8. [SetUp]
  9. public void SetUp()
  10. {
  11.  
  12. }
  13.  
  14. [TearDown]
  15. public void TearDown()
  16. {
  17.  
  18. }
  19. }
  20. }

当我尝试使用我的网站运行它时,它返回错误

“The currentthread needs to have its apartmentstate set to
ApartmentState.sta to be able to initiate Internet Explorer”

通常在使用除SetupFixture之外的任何东西时,需要将其作为解决方案.但由于某种原因,它现在不起作用.

解决方法

如果您包含该行,该解决方案实际上最终变得相当简单:
  1. [assembly: RequiresSTA]

页面顶部,它将设置整个程序集以使用STA,它不再抛出错误.

猜你在找的C#相关文章