Click here to Skip to main content
13,355,726 members (58,014 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;

namespace BadgerMeter.FieldSoftware.RouteReading
{
    public class NotifyWindowMessage
    {
        public event System.EventHandler OnStateChange;
        private WindowSink windowSink;
        private int uID = 5000;
        public bool notifyDev = false;
        public NotifyDevice nDev;

        public NotifyWindowMessage()
        {
            windowSink = new WindowSink(this);
            windowSink.uID = uID;
            windowSink.nDev = notifyDev;
        }

        ~NotifyWindowMessage()
        {
            //  Remove();
        }

        #region -- API Declarations-- 

        const Int32 DBT_DEVICEARRIVAL = 0X8000;
        const Int32 DBT_DEVICEREMOVECOMPLETE = 0X8004;
        const Int32 WM_DEVICECHANGE = 0X219;
        public static Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");

        #endregion

        #region class WindowSink
        internal class WindowSink : MessageWindow
        {
            private int m_uID = 0;
            public bool nDev;
            private NotifyWindowMessage notifyMsg;
            public WindowSink(NotifyWindowMessage notiMsg)
            {
                notifyMsg = notiMsg;
            }

            public int uID
            {
                set
                {
                    m_uID = value;
                }
            }

            protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
            {
                if (msg.Msg == WM_DEVICECHANGE)
                {
                }
                if ((msg.WParam.ToInt32() == DBT_DEVICEARRIVAL))
                {
                    notifyMsg.notifyDev = nDev = true;
                    if (notifyMsg.OnStateChange != null)
                        notifyMsg.OnStateChange(notifyMsg, null);

                }
                else if ((msg.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE))
                {
                    nDev = false;
                    MessageBox.Show("device removed");
                }
            }
        }
        #endregion
    }

  }

On your form or class add following.
public class MermoryStickWatcher : Form
   {
       public NotifyWindowMessage notifyMsg;
       public NotifyDevice notifyDevice;
       public MermoryStickWatcher()
       {
           notifyMsg = new NotifyWindowMessage();
           notifyMsg.OnStateChange += new EventHandler(OnStateChanged);
       }
  public void OnStateChanged(object sender, EventArgs e)
        {
          
            bool Attempt;
            if (notifyMsg.notifyDev)
            {
                DirectoryInfo dirInfo = new DirectoryInfo("/Hard Disk/Load/");
                //Check if Directory structure are there
                if (dirInfo.Exists)
                {
                    this.ValidateDirectory();
                }
                else
                {
                    //If Directory Structure are not there Foreign memory stick,then dont initialize, display a message for that   with yes no
                    Attempt = TextDialog.Show("Unrecognized memory stick! Do you want to Associate with Handheld? ",
                                                 "Unrecognized memory stick! ",
                                                 TextDialogButtons.YesNo) == TextDialogResult.Yes;
                    if (Attempt)
                    {
                        //Create File Structure
                        DirectoryInfo di = Directory.CreateDirectory("/Hard Disk/Load/");
                        this.ValidateDirectory();
                    }

                }
            }
        }
}
Posted 25-Jan-13 8:26am
G_Coder876
Updated 25-Jan-13 11:19am
v3
Comments
richcb 25-Jan-13 14:22pm
   
There is no question with this thread. Use the "Improve Question" widget to elaborate on your issue and describe a specific problem you are havning. No one can provide any sort of help without more information.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100