2017-05-23 43 views
0

我遇到的奇怪问题的排序。使用VB.net关闭文件夹 - 当IE打开时不起作用

林下面的代码找到一个打开的文件夹并将其关闭

Dim sh = CreateObject("shell.application") 
For Each item In sh.Windows 
    If item.document.folder.self.Path = DBFolder Then 
     item.Quit() 
    End If 
Next 

这里的怪异的一部分,它按预期工作,除非Internet Explorer窗口已打开。与IE浏览器打开我得到以下错误:

An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll

Additional information: Public member 'folder' on type 'HTMLDocumentClass' not found.

我个人不使用IE浏览器,但我的用户很可能这样做这是要引起问题,如果我部署。有没有人有解决方法或建议?

+1

打开**选项严格在**和修正错误。然后摆脱旧的vb6 ** CreateObject **。然后修复这些错误,应该是“迟绑定”错误。使用CreateObject的替代品,它是System.Activator。 CreateInstance方法。 – Codexer

+0

@Codexer他的问题是他正在循环浏览器和Internet Explorer窗口。 IE窗口没有“item.document.folder”成员。 –

+0

我知道确切的问题是什么,但他需要先解决其他问题。如果他这样做,那么调试器会向他显示问题。你错过了我的评论... – Codexer

回答

0

这是预期的行为,你将会拿起任何打开的资源管理器或Internet Explorer窗口。您可以通过在Microsoft Internet Object Library中添加对mshtml的引用以及在Microsoft Internet Controls中的SHDocVw来处理此问题。

Add Reference > COM > Type Libraries > Microsoft HTML Object Library

Add Reference > COM > Type Libraries > Microsoft Internet Controls

Dim sh As New SHDocVw.ShellWindows 

    For Each item In sh 

     If Not TypeOf item.Document Is mshtml.HTMLDocument Then 
      MessageBox.Show("it is not an IE window! " + item.Path()) 
     Else 
      MessageBox.Show("it is an IE window!" + item.Path()) 
     End If 


    Next item 
+0

Theres不需要添加所有这些,他已经有了这个对象。为什么不先检查对象的类型?如果它的HTMLDocumentClass跳过它......这显然不是一个目录。 – Codexer

+0

@Codexer也许我在这里失去了一些东西..他需要ShDocVw,所以他可以实例化壳并摆脱CreateObject(),他需要mshtml,因此他可以检查item.Document的类型 –

相关问题