Can we determine at runtime if app is Console or Windows?

T

Tom Leylan

Hi all, and to Cor, Herfried and the other "old folks", it's been a long
time :)

I might be hanging around again for awhile but in the meantime I have a
question. Does anybody know of a way to determine at runtime whether the
app is a console or a GUI app? I have to imagine the information is
available but searching around didn't yield the answer.

Thanks,
Tom
 
C

Cor Ligthert [MVP]

Hi Tom,

Good to see you back, in my idea is this a typical Herfried question.
I am almost sure he knows this.

Cor
 
T

Tom Leylan

Hi to you too... (you're an MVP now)!

It might sound a little funny since it must be known at compile time whether
it is a console app. On the other hand I was hoping that I might develop a
few routines that would adapt depending upon whether it was a console app,
so I would like to check at runtime. It might not turn out to be too useful
but I thought I'd check.

Anything else been going on?
 
O

Oenone

Tom said:
I might be hanging around again for awhile but in the meantime I have
a question. Does anybody know of a way to determine at runtime
whether the app is a console or a GUI app?

Seeing as no one else has answered, I'll offer you this solution. I don't
like it at all, but it does work (tested in VB2005):

\\\
Public Function IsConsole() As Boolean

Dim s As String

Try
s = Console.Title
Return True
Catch ex As System.IO.IOException
Return False
End Try

End Function
///

I'd strongly advise keeping this in a self-contained function, and then when
someone suggests a proper implementation you can just swap the code over. :)
 
T

Tom Leylan

That works well enough. I agree that catching the exception isn't the idea
method but the functionality is isolated. I added a static variable within
the function so it can be set once on the first call and subsequent calls
don't need to trip the exception again.

Thanks... it works!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top