If Not webBrowser1.Document Is Nothing Then Dim hdDOC As HtmlDocument = webBrowser1.Document Dim hecArray As HtmlElementCollection = hdDOC.GetElementsByTagName("h1")
-WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 Imports System.Management
Main
- 사용한 컨트롤 : ListView 1개, Button 2개
전체 소스 코드
Form1.vb
Imports System.Management
Public Class Form1
Dim mewWatcher As ManagementEventWatcher
Dim strUSBDriveName As String
Dim strUSBDriveLetter As String
Dim iCount As Integer = 0
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
CheckForIllegalCrossThreadCalls = False
End Sub
Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
MyBase.OnClosed(e)
If Not mewWatcher Is Nothing Then
mewWatcher.Stop()
mewWatcher.Dispose()
End If
End Sub
Sub mewWatcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
Dim mbo1 As ManagementBaseObject
Dim mbo2 As ManagementBaseObject
mbo1 = CType(e.NewEvent, ManagementBaseObject)
mbo2 = CType(mbo1("TargetInstance"), ManagementBaseObject)
Select Case mbo1.ClassPath.ClassName
Case "__InstanceCreationEvent"
If mbo2("InterfaceType").ToString() = "USB" Then
strUSBDriveName = mbo2("Caption").ToString()
strUSBDriveLetter = mbo2("Name").ToString()
Dim lvi As ListViewItem = New ListViewItem()
lvi.Text = (iCount + 1).ToString()
lvi.SubItems.Add(strUSBDriveName + " : " + strUSBDriveLetter + " 연결 되었습니다.")
listView1.Items.Add(lvi)
iCount += 1
End If
Case "__InstanceDeletionEvent"
If mbo2("InterfaceType").ToString() = "USB" Then
If mbo2("Caption").ToString() = strUSBDriveName Then
Dim lvi As ListViewItem = New ListViewItem()
lvi.Text = (iCount + 1).ToString()
lvi.SubItems.Add(strUSBDriveName + " : " + strUSBDriveLetter + " 해제 되었습니다.")
listView1.Items.Add(lvi)
iCount += 1
strUSBDriveLetter = ""
strUSBDriveName = ""
End If
End If
End Select
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'Start
'ManageMentEventWatcher 가 null 이면 생성...
If mewWatcher Is Nothing Then mewWatcher = New ManagementEventWatcher()
Dim weQuery As WqlEventQuery = New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " + "WHERE TargetInstance ISA 'Win32_DiskDrive'")
mewWatcher.Query = weQuery
'Event Create
AddHandler mewWatcher.EventArrived, AddressOf mewWatcher_EventArrived
mewWatcher.Start()
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
'Stop
If mewWatcher Is Nothing Then Return
mewWatcher.Stop()
mewWatcher.Dispose()
End Sub
End Class
-WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 using System.Management
Main
- 사용 컨트롤 : Label 1개, Button 1개
전체 소스 코드
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace CSharp_WMICpuSpeed
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (ManagementObject managementObject = new ManagementObject("Win32_Processor.DeviceID='CPU0'"))
{
uint ispeed = (uint)(managementObject["CurrentClockSpeed"]);
lbl0 .Text = ispeed.ToString();
}
}
}
}
Public Class Form1
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'원본 파일 선택...
Dim ofd As OpenFileDialog = New OpenFileDialog()
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
lblSource.Text = ofd.FileName
End If
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
'대상 파일 선택...
Dim ofd As OpenFileDialog = New OpenFileDialog()
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
lblDesc.Text = ofd.FileName
End If
End Sub
Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
Dim iFile1byte As Integer
Dim iFile2byte As Integer
Dim fs1 As System.IO.FileStream
Dim fs2 As System.IO.FileStream
'파일 위치 및 이름이 같으면...
If lblSource.Text = lblDesc.Text Then
lblCompare.Text = "같은 파일 입니다."
Return
End If
' Open the two files.
fs1 = New System.IO.FileStream(lblSource.Text, System.IO.FileMode.Open)
fs2 = New System.IO.FileStream(lblDesc.Text, System.IO.FileMode.Open)
'파일 길이 비교...
If fs1.Length <> fs2.Length Then
' Close the file
fs1.Close()
fs2.Close()
' Return false to indicate files are different
lblCompare.Text = "다른 파일 입니다."
Return
End If
Do
'Read one byte from each file.
iFile1byte = fs1.ReadByte()
iFile2byte = fs2.ReadByte()
Loop While ((iFile1byte = iFile2byte) And (iFile1byte <> -1))
' Close the files.
fs1.Close()
fs2.Close()
If (iFile1byte - iFile2byte) = 0 Then
lblCompare.Text = "같은 파일 입니다."
End If
End Sub
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CSharp_FileCompare
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//원본 파일 선택...
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
lblSource.Text = ofd.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
//대상 파일 선택...
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
lblDesc.Text = ofd.FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
//File Compare
int iFile1byte;
int iFile2byte;
System.IO.FileStream fs1;
System.IO.FileStream fs2;
//파일 위치 및 이름이 같으면...
if (lblSource.Text == lblDesc.Text)
{
lblCompare.Text = "같은 파일 입니다.";
return;
}
// Open the two files.
fs1 = new System.IO.FileStream(lblSource.Text, System.IO.FileMode.Open);
fs2 = new System.IO.FileStream(lblDesc.Text, System.IO.FileMode.Open);
//파일 길이 비교...
if (fs1.Length != fs2.Length)
{
// Close the file
fs1.Close();
fs2.Close();
// Return false to indicate files are different
lblCompare.Text = "다른 파일 입니다.";
return ;
}
do
{
// Read one byte from each file.
iFile1byte = fs1.ReadByte();
iFile2byte = fs2.ReadByte();
}
while ((iFile1byte == iFile2byte) && (iFile1byte != -1));
// Close the files.
fs1.Close();
fs2.Close();
if ((iFile1byte - iFile2byte) == 0)
{
lblCompare.Text = "같은 파일 입니다.";
}
}
}
}