<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://bloggingabout.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'devenv'</title><link>http://bloggingabout.net/search/SearchResults.aspx?a=1&amp;o=DateDescending&amp;tag=devenv&amp;orTags=0</link><description>Search results matching tag 'devenv'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>How to make .NET application support both console and GUI mode</title><link>http://bloggingabout.net/blogs/vagif/archive/2010/04/19/how-to-make-net-application-support-both-console-and-gui-mode.aspx</link><pubDate>Mon, 19 Apr 2010 18:47:48 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:483126</guid><dc:creator>VagifAbilov</dc:creator><description>&lt;p&gt;I am currently working on a next version of a &lt;a href="http://bloggingabout.net/blogs/vagif/archive/2009/12/03/utility-to-generate-solution-files-can-now-create-solution-folders.aspx" target="_blank"&gt;utility to generate Visual Studio solutions&lt;/a&gt;. A new version will be called Solution Maker and add user interface support to a command line option. I wanted to mix console and GUI mode in a single program, but strictly speaking this is impossible. A program is assigned its type at compile time, so any attempt to change it’s behavior at execution time does not alter the application’s nature. A console application may create Windows forms, and a Window-based application may allocate consoles, but they won’t become ducks even if they quack.&lt;/p&gt;  &lt;p&gt;But Visual Studio does it, don’t it? You can launch IDE and still you can run “devenv” from a command-line. How does it do it?&lt;/p&gt;  &lt;p&gt;Well, actually it doesn’t! &lt;a href="http://blogs.msdn.com/junfeng/archive/2004/02/06/68531.aspx" target="_blank"&gt;Here’s&lt;/a&gt; an explanation of the trick. There two binaries: devenv.com and devenv.exe. “Com” is always probed first, and this is the console one. So when you type “devenv” it’s a console version that will be executed. And if it does not get any command-line input, it simply launches devenv.exe. Smart!&lt;/p&gt;  &lt;p&gt;And this can be done with any application. Here are the steps:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Write an application with user interface, let’s say it’s called MyProgram.exe. This program should not have anything special. &lt;/li&gt;    &lt;li&gt;Write a command line application MyProgram.Command.exe. This program needs a few special lines, we will look at them. &lt;/li&gt;    &lt;li&gt;Rename MyProgram.Command.exe to MyProgram.com and copy it to the folder where GUI MyProgram.exe resides. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;That’s it. And of course, command-line version of MyProgram needs some special code. Here it is:&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;class Program
{
    static void Main(string[] args)
    {
        string relayProcessPath = null;

        // Only relay to another application if no command-line arguments are specified
        if (args.Length == 0)
        {
            string thisProcessPath = Process.GetCurrentProcess().MainModule.FileName;
            if (Path.GetExtension(thisProcessPath).ToLower() == &amp;quot;.com&amp;quot;)
            {
                // Relay process should only differ in extension
                string expectedRelayPath = Path.ChangeExtension(thisProcessPath, &amp;quot;.exe&amp;quot;);
                if (File.Exists(expectedRelayPath))
                {
                    relayProcessPath = expectedRelayPath;
                }
            }
        }

        if (!string.IsNullOrEmpty(relayProcessPath))
        {
            // Launch relay process
            Process.Start(relayProcessPath);
        }
        else
        {
            // Process with a command line
            Console.WriteLine(&amp;quot;Hello from command line&amp;quot;);
        }
    }
}&lt;/pre&gt;</description></item></channel></rss>