Get Startup path, application name and vendor

image_pdfimage_print


   
 
  using System;
  using System.Windows.Forms;
  using Microsoft.Win32;

  public class MyMessageFilter : IMessageFilter 
  {
    public bool PreFilterMessage(ref Message m) 
    {
      // Intercept the left mouse button down message.
      if (m.Msg == 513) 
      {
        Console.WriteLine("WM_LBUTTONDOWN is: " + m.Msg);
        return true;
      }
      return false;
    }
  }

  public class mainForm : System.Windows.Forms.Form
  {
    private MyMessageFilter msgFliter = new MyMessageFilter();

    public mainForm()
    {
      GetStats();
      Application.ApplicationExit += new EventHandler(Form_OnExit);
      Application.AddMessageFilter(msgFliter);    
    }

    [STAThread]
    static void Main() 
    {
      Application.Run(new mainForm());
    }

    private void GetStats()
    {
      Console.WriteLine(Application.CompanyName+ "  Company:");
      Console.WriteLine(Application.ProductName+ " App Name:");
      Console.WriteLine(Application.StartupPath);
    }

    // Event handlers.
    private void Form_OnExit(object sender, EventArgs evArgs) 
    {
      Console.WriteLine("Exit", "This app is dead...");
      Application.RemoveMessageFilter(msgFliter);
    }
  }



           
         
     


Create DLL library

image_pdfimage_print
   


// DllTestServer.cs
// build with the following command line switches
//     csc /t:library DllTestServer.cs
public class DllTestServer
{
 public static void Foo()
 {
  System.Console.WriteLine("DllTestServer.Foo (DllTestServer.DLL)");
 }
}
//////////////////////////////////////////////////////////
// DllTestClient.cs
// build with the following command line switches
//     csc DllTestClientApp.cs /r:DllTestServer.dll 
using System;
using System.Diagnostics;
using System.Reflection;

class DllTestClientApp
{
    public static void Main()
    {
        Assembly DLLAssembly = Assembly.GetAssembly(typeof(DllTestServer));
        Console.WriteLine("
DllTestServer.dll Assembly Information"); 
        Console.WriteLine("	" + DLLAssembly);

        Process p = Process.GetCurrentProcess();
        string AssemblyName = p.ProcessName + ".exe";
        Assembly ThisAssembly = Assembly.LoadFrom(AssemblyName);
        Console.WriteLine("DllTestClient.exe Assembly Information"); 
        Console.WriteLine("	" + ThisAssembly + "
");

  Console.WriteLine("Calling DllTestServer.Foo...");
  DllTestServer.Foo();
    }
}

           
          


Load system dll library

image_pdfimage_print
   


using System;
using System.Reflection;

class Class1 {
   static void Main(string[] args) {
      string windir = Environment.GetEnvironmentVariable("windir");
      Assembly ass = Assembly.LoadFrom(windir + @"Microsoft.NETFrameworkv1.1.4322System.Drawing.dll");
      foreach (Type type in ass.GetTypes()) {
         Console.WriteLine(type.ToString());
      }
   }
}

           
          


Compile cs file to DLL

image_pdfimage_print
   
 
// Math.cs
   public class Math
   {
      ///<summary>
      ///   The Add method allows us to add two integers
      ///</summary>
      ///<returns>Result of the addition (int)</returns>
      ///<param name="x">First number to add</param>
      ///<param name="y">Second number to add</param>
      public int Add(int x, int y)
      {
         return x + y;
      }
   }
//csc /t:library /doc:Math.xml Math.cs

    


Using Switches to Control Debug and Trace:User-Defined Switch

image_pdfimage_print
   

// compile with: csc /r:system.dll file_1.cs

using System;
using System.Diagnostics;

enum SpecialSwitchLevel
{
    Mute = 0,
    Terse = 1,
    Verbose = 2,
    Chatty = 3
}

class SpecialSwitch: Switch
{
    public SpecialSwitch(string displayName, string description) :
    base(displayName, description)
    {
    }
    
    public SpecialSwitchLevel Level
    {
        get
        {
            return((SpecialSwitchLevel) base.SwitchSetting);
        }
        set
        {
            base.SwitchSetting = (int) value;
        }    
    }
    public bool Mute
    {
        get
        {
            return(base.SwitchSetting == 0);
        }
    }
    
    public bool Terse
    {
        get
        {
            return(base.SwitchSetting >= (int) (SpecialSwitchLevel.Terse));
        }
    }
    public bool Verbose
    {
        get
        {
            return(base.SwitchSetting >= (int) SpecialSwitchLevel.Verbose);
        }
    }
    public bool Chatty
    {
        get
        {
            return(base.SwitchSetting >=(int) SpecialSwitchLevel.Chatty);
        }
    }
    
    protected new int SwitchSetting
    {
        get
        {
            return((int) base.SwitchSetting);
        }
        set
        {
            if (value < 0)
            value = 0;
            if (value > 4)
            value = 4;
            
            base.SwitchSetting = value;
        }
    }
}

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Console.WriteLine("VerifyState");
        Debug.WriteLineIf(debugOutput.Terse, "VerifyState Start");
        
        Debug.WriteLineIf(debugOutput.Chatty, 
        "Starting field verification");
        
        if (debugOutput.Verbose)
        Debug.WriteLine("VerifyState End");
    }
    
    static SpecialSwitch    debugOutput = 
    new SpecialSwitch("MyClassDebugOutput", "application");
    int i = 0;
}

public class TraceUserDefinedSwitch
{
    public static void Main()
    {
        //TraceSwitch ts = new TraceSwitch("MyClassDebugOutput", "application");
        //Console.WriteLine("TraceSwitch: {0}", ts.Level);
        
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}