2011年8月31日 星期三

[C#] 監控USB插拔 2

先前也寫過一篇 監控 USB 插拔的文章,見 http://kuomingwang.blogspot.com/2010/09/c-usb.html
但是那是監控已知的 USB 儲存設備,最近又有類似的需求,這算是另一種寫法。

//using System.Management;

ManagementEventWatcher mew = null;

         private void Form1_Load(object sender, EventArgs e)
        {
            mew = new ManagementEventWatcher("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE TargetInstance ISA \"Win32_DiskDrive\"");
            mew.Start();
            mew.EventArrived += new EventArrivedEventHandler(mew_go);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            mew.Stop();
        }
        private void mew_go(object sender, System.Management.EventArrivedEventArgs e)
        {
            ManagementBaseObject newEvent = e.NewEvent, newEventTarget = (newEvent["TargetInstance"] as ManagementBaseObject);
            if (newEventTarget["InterfaceType"].ToString() == "USB")
            {
                switch (newEvent.ClassPath.ClassName)
                {
                    case "__InstanceCreationEvent":
                        Listbox_text_Update(listBox1, Convert.ToString(newEventTarget["Caption"])+" 裝置已插入");
                        break;
                    case "__InstanceDeletionEvent":
                        Listbox_text_Update(listBox1, Convert.ToString(newEventTarget["Caption"])+" 裝置已退出");
                        break;
                }
            }
        }
        public static void Listbox_text_Update(ListBox lbox, string s)
        {
            if (lbox.InvokeRequired)
            {
                lbox.BeginInvoke(new MethodInvoker(() => Listbox_text_Update(lbox, s)));
            }
            else
            {
                lbox.Items.Add(s);
            }
        }


2011年8月17日 星期三

[C#] 如何變更本機電腦日期、變更本機電腦時間,來作 時間同步校正

主要需求是因為 有些 Client 端 PC 都是服役多年的老 PC 所以 BIOS 電池都沒力了,又懶得一台一台換。

所以直接從 SQL Server 端 作時間的校正。

 [DllImport("Kernel32.dll")]
 private extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);

 [StructLayout(LayoutKind.Sequential)]
        private struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }

 public void SetTime(DateTime SqlServerTime)
        {
            SYSTEMTIME st = new SYSTEMTIME();
            st.wYear = Convert.ToUInt16(SqlServerTime.Year);
            st.wMonth = Convert.ToUInt16(SqlServerTime.Month);
            st.wDay = Convert.ToUInt16(SqlServerTime.Day);
            st.wHour = Convert.ToUInt16(SqlServerTime.Hour);
            st.wMilliseconds = Convert.ToUInt16(SqlServerTime.Millisecond);
            st.wMinute = Convert.ToUInt16(SqlServerTime.Minute);
            st.wSecond = Convert.ToUInt16(SqlServerTime.Second);
            SetLocalTime(ref st);
        }

//使用時
  ChangeLocalDateTime CLDT = new ChangeLocalDateTime();
 CLDT.SetTime(db_dt); //db_dt 是指從 SQL Server 取回來的 GETDATE()