Platform Helper

   
 
// Taken from....
//
// NAnt - A .NET build tool
// Copyright (C) 2001-2003 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// Jaroslaw Kowalski (jkowalski@users.sourceforge.net)

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Dotnet.Commons.IO {
    [Serializable()]
    internal class PlatformHelper {
        public static readonly bool IsMono;
        public static readonly bool IsWin32;
        public static readonly bool IsUnix;
        public static readonly bool PInvokeOK;
        
        static PlatformHelper() {
            // check a class in mscorlib to determine if we're running on Mono
            if (Type.GetType("System.MonoType", false) != null) {
                // we're on Mono
                IsMono = true;
            }
            
            PlatformID platformID = Environment.OSVersion.Platform;

            if (platformID == PlatformID.Win32NT || platformID == PlatformID.Win32Windows) {
                IsWin32 = true;
            }

            // check for (non-)Unix platforms - see FAQ for more details
            // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
            int platform = (int) Environment.OSVersion.Platform;
            if (platform == 4 || platform == 128) {
                IsUnix = true;
            }

            if (IsWin32 && !IsMono) {
                PInvokeOK = true;
            }
        }
      
        private class PInvokeHelper {
            [DllImport("kernel32.dll")]
            private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);

            public static long GetVolumeInformationWrapper(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize) {
                return GetVolumeInformation(PathName, VolumeNameBuffer, VolumeNameSize, ref VolumeSerialNumber, ref MaximumComponentLength, ref FileSystemFlags, FileSystemNameBuffer, FileSystemNameSize);
            }
        }
    }
}

   
     


StartupPath

   
  

using System;
using System.Windows.Forms;

class MainClass {
    static void Main(string[] args) {

        OpenFileDialog dlg = new OpenFileDialog();
        dlg.InitialDirectory = Application.StartupPath;

        if (dlg.ShowDialog() == DialogResult.OK) {
            Console.WriteLine(dlg.FileName);

        }
    }
}

   
     


Removing an Installed Message Filter

   
 


using System;
using System.Windows.Forms;
   
public class BlockLeftMouseButtonMessageFilter : IMessageFilter
{
    const int WM_LBUTTONDOWN = 0x201;
    
    public bool PreFilterMessage(ref Message m){
        if(m.Msg == WM_LBUTTONDOWN)
        {
            Console.WriteLine("The left mouse button is down.");
            Application.RemoveMessageFilter(this);
            return true;
        }
        return false;
    }
}
   
public class MainForm : Form
{
    public static void Main()
    {
        MainForm MyForm = new MainForm();
        BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter();
        Application.AddMessageFilter(MsgFilter);
        Application.Run(MyForm);
    }
   
    public MainForm()
    {
        Text = "Message Filter Removal Test";
    }
}

    


Installing a Message Filter

   
 


using System;
using System.Windows.Forms;
   
public class BlockLeftMouseButtonMessageFilter : IMessageFilter
{
    const int WM_LBUTTONDOWN = 0x201;
    const int WM_LBUTTONUP = 0x202;
    
    public bool PreFilterMessage(ref Message m)
    {
        if(m.Msg == WM_LBUTTONDOWN)
        {
            Console.WriteLine("The left mouse button is down.");
            return true;
        }
        if(m.Msg == WM_LBUTTONUP)
        {
            Console.WriteLine("The left mouse button is up.");
            return true;
        }
        return false;
    }
}
   
public class MainForm : Form
{
    public static void Main()
    {
        MainForm MyForm = new MainForm();
        BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter();
   
        Application.AddMessageFilter(MsgFilter);
        Application.Run(MyForm);
    }
   
    public MainForm()
    {
        Text = "Message Filter Test";
    }
}

    


Handling Application Events: On Thread Exit


   


using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
   

public class HelloWorldForm : Form
{
    public HelloWorldForm()
    {
        Text = "Hello, WindowsForms!";
    }
}
   
public class ApplicationEventHandlerClass
{
   
    public void OnThreadExit(object sender, EventArgs e)
    {
        Console.WriteLine("The application's main thread is shutting down.");
    }
}
   
public class MainClass
{
    public static void Main()
    {
        HelloWorldForm FormObject = new HelloWorldForm();
        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
   
        Application.ThreadExit += new EventHandler(AppEvents.OnThreadExit);
        Application.Run(FormObject);
    }
}
           
          


Handling Application Events: Thread Exception


   

using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
   

public class HelloWorldForm : Form
{
    public HelloWorldForm()
    {
        Text = "Hello, WindowsForms!";
    }
}
   
public class ApplicationEventHandlerClass
{
   
    public void OnThreadException(object sender, ThreadExceptionEventArgs e)
    {
        Console.WriteLine("an exception thrown from an application thread was caught!");
    }
}
   
public class MainClass
{
    public static void Main()
    {
        HelloWorldForm FormObject = new HelloWorldForm();
        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
   
        Application.ThreadException += new ThreadExceptionEventHandler(AppEvents.OnThreadException);
        Application.Run(FormObject);
    }
}
           
          


Handling Application Events: On Idle


   

using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
   

public class HelloWorldForm : Form
{
    public HelloWorldForm()
    {
        Text = "Hello, WindowsForms!";
    }
}
   
public class ApplicationEventHandlerClass
{
    public void OnIdle(object sender, EventArgs e)
    {
        Console.WriteLine("The application is idle.");
    }
}
   
public class MainClass
{
    public static void Main()
    {
        HelloWorldForm FormObject = new HelloWorldForm();
        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
   
        Application.Idle += new EventHandler(AppEvents.OnIdle);
        Application.Run(FormObject);
    }
}