0

I am having a file in my project. This file is used by some other projects. I want to show some messages in this file using console for console projects, and Messagebox for Windows Application projects. So, I want to know the project type programatically.Can anybody say how to achieve this?

Note: The file is in C# language.

EDIT: David's answer is the way I have to follow. But, currently we are near to release date. So, I need to use the project type as temporary solution. Is there any way for that?

prabhakaran
  • 5,126
  • 17
  • 71
  • 107
  • 3
    This sounds like a design problem with the code, honestly. The common code shouldn't be coupled with the application implementation in any way. It should just produce the message itself (a string, I assume) and then the application-specific code should handle actually displaying it. – David Jul 07 '12 at 09:51
  • @David What about Exception Handling – prabhakaran Jul 07 '12 at 09:53
  • What _about_ it? You can still catch and handle exceptions anywhere in your code. What makes you think that you can't? – David Jul 07 '12 at 09:54
  • @David Here I am catching the Mysql exception in the mysql-object method. This is called by another method. That method is called by another method (totally 3 ). In this kind of situation implementing string for each exception on the calling side will make the code messy. Can you say how to implement the code uncoupled and also do avoid the mess. – prabhakaran Jul 07 '12 at 10:00
  • When you catch the MySql exception, what do you do with it? In general, without knowing about your overall domain, I would recommend that you catch the MySql exception, log any useful information relating to the error, and then throw another exception (preferably a custom domain exception) from within the shared code. The application(s) can intercept that exception (since it makes more sense for an application sitting on top of a custom domain to catch a custom exception than a MySql infrastructure exception) and handle it accordingly (such as display a message). – David Jul 07 '12 at 10:11

1 Answers1

0

Test if there is a Main windows, if not you must be in a console application :

if (Process.GetCurrentProcess().MainWindowHandle == IntPtr.Zero)
{
    Console.WriteLine(...);
}
else
{
    // Your Message box code
    ....
}
Julien Ch.
  • 1,231
  • 9
  • 16
  • The issue is also debated [here](http://stackoverflow.com/questions/6408588/how-to-tell-if-there-is-a-console) – Julien Ch. Jul 07 '12 at 12:02
  • Sure it doesn't distinct a no window app from a NT service, but the question was about a console app or a windowed app. – Julien Ch. Jul 07 '12 at 13:54