Creativity, Innovation... Failure



Execute process from C# and reading output

System.Diagnostics.Process csh = new System.Diagnostics.Process();
csh.StartInfo.FileName ="EXECUTABLE.EXE";
csh.StartInfo.Arguments = @"YOUR ARGUMENTS";
csh.StartInfo.CreateNoWindow = false;
csh.StartInfo.RedirectStandardOutput = true;
csh.StartInfo.UseShellExecute = false;
csh.Start();
csh.WaitForExit();

string output = csh.StandardOutput.ReadToEnd();
// Do something with the output.

Detecting design mode

Lets say you've just created a user control or customized an existing control from the .Net Framework and you want the bahavior to be different when you're in design mode vs execution mode. How can you do it?

Solution #1: DesignMode Property 

The first an very intuitive approach is to use the DesignMode property of your control. The problem is that it will always return false if it has a parent... not so great

Solution #2: Design Mode Property with a twist 

The solution then is to go up the control hierarchy and find the parent with the right value!
The resulting code will look a little like this:

public bool IsDesignMode ()
{
    Control instance = this;
    while (instance != null)
    {
        if (instance.Site == null) return false;
        if (instance.Site.DesignMode) return true;
        instance = instance.Parent;
    }
    return false;
}

Using this method can prevent errors where the GUI does not draw properly in design mode because it tries to access data (i.e. database connection)
The biggest limitation is that you can't call the method from the constructor because the parent may not be set yet!

Of course there are other solutions but they are not as elegant. Here's one of them:

bool designMode = Application.CompanyName == "Microsoft Corporation";

This solution works from the constructor, so it does have its advantages...


Using code snippets in C#

I know code snippets have been out there for a while but I haven't had the chance to use them until today. And quite frankly, I should have checked them out before! The default code snippets are interesting (ctor, for, foreach, forr, switch to name a few) but their real power comes from the possibility of adding new ones almost instantly to your work environment.

 Here's a code snippet I did for the Singleton Pattern:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> 
<CodeSnippet Format="1.0.0"> 
    <Header>  
       
<Title>Singleton Pattern</Title>  
       
<Shortcut>Singleton</Shortcut>  
       
<Description>Code snippet to define Singleton class</Description>  
       
<Author>marivet</Author>  
       
<SnippetTypes>    
            
<
SnippetType>Expansion</SnippetType>
  
       
</SnippetTypes> 
   
</Header> 
   
<Snippet>  
       
<
Declarations>
   
           
<Literal Editable="false">    
               
<
ID>Class</ID>
     
                
<ToolTip>Type</ToolTip>    
                <
Function>ClassName()</Function>
     
                <
Default>SingletonClass</Default>
    
            </
Literal>
   
       
</
Declarations>
  
        <
Code Language="csharp">
   
        <![CDATA[
  
  #region Singleton Pattern
  private $Class$()
  {
  
  }
  private static $Class$ _instance = null;
  public static $Class$ Instance
  {
   get
   {
    if (_instance == null)
     _instance = new $Class$();
    return _instance;
   }
  }
  #endregion
  $end$
  
       
</Code> 
    </Snippet>
</CodeSnippet>
</CodeSnippets>

Copy this into a .snippet file and put it in your MyDocument/Visual Studio 2005/Code Snippets/Visual C#/My Code Snippets folder and you're ready to go!

Enjoy Smile