How to get title of application/console window

M

mike_j_s

I thought this was going to be a simple question with a simple
solution, but I haven't been able to find an elegant way to accomplish
this. I have event logging code in a DLL assembly that is shared by
GUI applications and console applications. Events are being logged to
the Windows Event Log, and I would like to use the title of the
application as the "source" of each event. Naturally, this means that
for a GUI app, I need to get the title of the main window, and for a
console app, I need to get the title of the console window
("Console.Title"). What is the best way to accomplish this? I
haven't even been able to figure out a way to determine if the running
application is a GUI or console app. I know that I can access
"Console.Title" and if it throws an exception I can assume it's a GUI
application, but I was hoping for something more elegant.

Any ideas? Thank you in advance for your help.

- Mike
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

I don't think that this is a good idea, because you are then making the
implicit declaration that you can not change the title of your application,
which quite frankly, is unreasonable. If it were reasonable, then the Text
property on the Form class would be read-only and you wouldn't be able to
change it.

I think it is a better idea for the code calling your logging code to
provide an application name, through code, or maybe through a configuration
file (you can have the application name set in a section for your settings),
and then use that.
 
Z

zacks

I thought this was going to be a simple question with a simple
solution, but I haven't been able to find an elegant way to accomplish
this. I have event logging code in a DLL assembly that is shared by
GUI applications and console applications. Events are being logged to
the Windows Event Log, and I would like to use the title of the
application as the "source" of each event. Naturally, this means that
for a GUI app, I need to get the title of the main window, and for a
console app, I need to get the title of the console window
("Console.Title"). What is the best way to accomplish this? I
haven't even been able to figure out a way to determine if the running
application is a GUI or console app. I know that I can access
"Console.Title" and if it throws an exception I can assume it's a GUI
application, but I was hoping for something more elegant.

Any ideas? Thank you in advance for your help.

- Mike

Assuming the event logging code in the DLL is implemented as a class
object in the calling application, why not just make a property in the
DLL for "window title", and then after you instantiate the new class
object, set the property from the appropriate property in the calling
application before you invoke the method that actully does the
logging.
 
M

mike_j_s

Nicholas,

Thank you for your response. This may be poor form, but I actually
*depend* on the fact that the title of the applications change,
because then the event source provides more information about the
state of the application that logged the event. For example, one of
the console apps starts with a title of "ICBCTask", and then it
attempts to parse an integer ID that was passed in as a command line
parm. If it successfully parses the ID, then it changes the title to
"ICBC - Task ID xx". The app then attempts to connect to a database
and reads a task description from a table using the ID. If it
succeeds, then it changes the title to "ICBC - Unload Task" (or
whatever the description is). By looking at the source in the event
log, I can get more information about which task actually logged the
event without having to view the detailed event information. This
could be useful, because there can be 15 or 16 instances of this
console app running simultaneously, with each one processing a
different task.

But maybe it's better to keep the source consistent (e.g., "ICBCTask")
for all instances of the console app that are running, and provide all
of the details in the event message instead? I just figured it would
be easier to sort out the events for a specific task if the source
described the task as accurately as possible.

- Mike

Mike,

I don't think that this is a good idea, because you are then making the
implicit declaration that you can not change the title of your application,
which quite frankly, is unreasonable. If it were reasonable, then the Text
property on the Form class would be read-only and you wouldn't be able to
change it.

I think it is a better idea for the code calling your logging code to
provide an application name, through code, or maybe through a configuration
file (you can have the application name set in a section for your settings),
and then use that.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I thought this was going to be a simple question with a simple
solution, but I haven't been able to find an elegant way to accomplish
this. I have event logging code in a DLL assembly that is shared by
GUI applications and console applications. Events are being logged to
the Windows Event Log, and I would like to use the title of the
application as the "source" of each event. Naturally, this means that
for a GUI app, I need to get the title of the main window, and for a
console app, I need to get the title of the console window
("Console.Title"). What is the best way to accomplish this? I
haven't even been able to figure out a way to determine if the running
application is a GUI or console app. I know that I can access
"Console.Title" and if it throws an exception I can assume it's a GUI
application, but I was hoping for something more elegant.
Any ideas? Thank you in advance for your help.
- Mike- Hide quoted text -

- Show quoted text -
 
M

mike_j_s

z,

Good idea. Better yet, I could just pass in the source description to
the class constructor...that should suit my purposes. I was trying to
make the code as generic as possible, but maybe it's just better this
way. :)

Thanks,
Mike
 

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