Richard H Richard H - 3 months ago 25
Vb.net Question

vb console application does not close

I created a vb.net to grab an attribute from AD and create a local variable.
It does that just fine, the problem is that it leaves the console window open with nothing but a flashing cursor. Ideally it would close as soon as it created the variable.

I've tried adding Environment.Exit (0)

Module Module1

Sub Main()
Dim objShell
Dim objUserEnv
Dim objADSysInfo
Dim objUser

objShell = CreateObject("WScript.Shell")
objUserEnv = objShell.Environment("USER")
objADSysInfo = CreateObject("ADSystemInfo")
objUser = GetObject("LDAP://" & objADSysInfo.UserName)
' This will create the variable %ipphone%
objUserEnv("ipphone") = objUser.ipPhone

End Sub

End Module


Any ideas why it's not closing the console?

Answer

Ok, Since Environment.Exit(0) is not working and you are within a Console application, Try this,

Go to Project > Add Reference > Assemblies > Framework and search for System.Windows.Forms click the check box and then click the Ok after that add this to your code.

Imports System.Windows.Forms
Module Module1

Sub Main()

    Dim objShell
    Dim objUserEnv
    Dim objADSysInfo
    Dim objUser

    objShell = CreateObject("WScript.Shell")
    objUserEnv = objShell.Environment("USER")
    objADSysInfo = CreateObject("ADSystemInfo")
    objUser = GetObject("LDAP://" & objADSysInfo.UserName)
    ' This will create the variable %ipphone%
    objUserEnv("ipphone") = objUser.ipPhone
    Application.Exit()
End Sub
End Module

Another reason that it might not be closing is because it is not completing the task, When I ran your code, It didnt complete and I got this error

Additional information: No mapping between account names and security IDs was done. (Exception from HRESULT: 0x80070534)

Since you didn't mention this in your question, Im guessing you didnt get this error?

Comments