반응형

* VBNET WebBrowser 컨트롤을 이용한 외부 IP 얻어 오기 예제...

 

Main

 

-사용한 컨트롤 : WebBrowser 1개, Label 1개, Button 1개

 

전체 소스 코드

Form1.vb

 

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        Dim strURL As String = "http://www.findip.kr/"

        '스크립트 오류 무시 하기...
        webBrowser1.ScriptErrorsSuppressed = True
        webBrowser1.Navigate(strURL)

    End Sub

    Private Sub webBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles webBrowser1.DocumentCompleted

        If Not webBrowser1.Document Is Nothing Then
            Dim hdDOC As HtmlDocument = webBrowser1.Document
            Dim hecArray As HtmlElementCollection = hdDOC.GetElementsByTagName("h1")

            label1.Text = hecArray(0).OuterHtml

        End If

    End Sub

End Class

 

 

위 소스 코드에 보듯이 외부 사이트에 접속 해서 외부 IP 를 얻어 오는 방법으로 webBrower 컨트롤 로 외부 사이트 접속

-> 사이트 html 을 다운 하여 태그에서 얻어 오는 걸 볼 수 있습니다.

HtmlElementCollection 변수에 바로 배열 인덱스 0 을 쓴 이유는 h1 태그가 하나 밖에 없어서 이며, 혹 h1 태그가 여러개가 되면 for 문 또는 for each 문을 사용하여 사용자가 확인 후 걸러낼 수 있습니다. 

 

아래의 그림처럼 구글 크롬으로 외부 사이트 접속 하여 상단 메뉴에 개발자 도구를 선택. 붉은 테투리 의 h1 태그 이름을

가져 옵니다. 

 

 

*예제 결과

 

결과 화면



https://kdsoft-zeros.tistory.com/183

 

[C#] [Control] WebBrowser 컨트롤 - 외부 IP 알아내기

* C# WebBrowser 컨트롤을 이용한 외부 IP 알아내기 예제... -사용한 컨트롤 : WebBrowser 1개, Label 1개, Button 1개 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System...

kdsoft-zeros.tistory.com

 

반응형
반응형

* C# WebBrowser 컨트롤을 이용한 외부 IP 알아내기 예제...

 

Main

 

-사용한 컨트롤 : WebBrowser 1개, 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;

namespace CSharp_외부IP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strURL = "http://www.findip.kr/";
            
            //스크립트 오류 무시 하기...
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Navigate(strURL);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.Document != null)
            {
                HtmlDocument hdDoc = webBrowser1.Document;
                HtmlElementCollection hecArray = hdDoc.GetElementsByTagName("h1");

                label1.Text = hecArray[0].OuterHtml;

            }
        }
    }
}

 

위 소스 코드에 보듯이 외부 사이트에 접속 해서 외부 IP 를 얻어 오는 방법으로 webBrower 컨트롤 로 외부 사이트 접속

-> 사이트 html 을 다운 하여 태그에서 얻어 오는 걸 볼 수 있습니다.

HtmlElementCollection 변수에 바로 배열 인덱스 0 을 쓴 이유는 h1 태그가 하나 밖에 없어서 이며, 혹 h1 태그가 여러개가 되면 for 문 또는 for each 문을 사용하여 사용자가 확인 후 걸러낼 수 있습니다. 

 

아래의 그림처럼 구글 크롬으로 외부 사이트 접속 하여 상단 메뉴에 개발자 도구를 선택. 붉은 테투리 의 h1 태그 이름을

가져 옵니다. 

 

 

*예제 결과

 

결과 화면

 

https://kdsoft-zeros.tistory.com/184

 

[VBNET] [Control] WebBrowser 컨트롤 - 외부 IP 알아내기

* VBNET WebBrowser 컨트롤을 이용한 외부 IP 얻어 오기 예제... Main -사용한 컨트롤 : WebBrowser 1개, Label 1개, Button 1개 전체 소스 코드 Form1.vb Public Class Form1 Private Sub button1_Click(ByVal s..

kdsoft-zeros.tistory.com

 

반응형
반응형

* VBNET WMI 를 이용한 USB 연결 및 연결해제 예제...

- 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

 

위 구문에서 보듯이 CheckfoCheckForIllegalCrossThreadCalls 를 사용 (크로스 스레드 예제 https://kdsoft-zeros.tistory.com/23 참조)

 

- 아래 클래스에 붉은색 표시 -> 소스코드에 사용한 필드 !!

 

[Dynamic, Provider("CIMWin32"), UUID("{8502C4B2-5FBB-11D2-AAC1-006008C78BC7}"), AMENDMENT]
class Win32_DiskDrive : CIM_DiskDrive
{
  uint16   Availability;
  uint32   BytesPerSector;
  uint16   Capabilities[];
  string   CapabilityDescriptions[];
  string   Caption;
  string   CompressionMethod;
  uint32   ConfigManagerErrorCode;
  boolean  ConfigManagerUserConfig;
  string   CreationClassName;
  uint64   DefaultBlockSize;
  string   Description;
  string   DeviceID;
  boolean  ErrorCleared;
  string   ErrorDescription;
  string   ErrorMethodology;
  string   FirmwareRevision;
  uint32   Index;
  datetime InstallDate;
  string   InterfaceType;
  uint32   LastErrorCode;
  string   Manufacturer;
  uint64   MaxBlockSize;
  uint64   MaxMediaSize;
  boolean  MediaLoaded;
  string   MediaType;
  uint64   MinBlockSize;
  string   Model;
  string   Name;
  boolean  NeedsCleaning;
  uint32   NumberOfMediaSupported;
  uint32   Partitions;
  string   PNPDeviceID;
  uint16   PowerManagementCapabilities[];
  boolean  PowerManagementSupported;
  uint32   SCSIBus;
  uint16   SCSILogicalUnit;
  uint16   SCSIPort;
  uint16   SCSITargetId;
  uint32   SectorsPerTrack;
  string   SerialNumber;
  uint32   Signature;
  uint64   Size;
  string   Status;
  uint16   StatusInfo;
  string   SystemCreationClassName;
  string   SystemName;
  uint64   TotalCylinders;
  uint32   TotalHeads;
  uint64   TotalSectors;
  uint64   TotalTracks;
  uint32   TracksPerCylinder;
};

 

*예제 결과

 

- 연결된 모습

 

-연결 해제된 모습

 

* 참조 (마이크로소프트)

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-diskdrive



https://kdsoft-zeros.tistory.com/181

 

[C#] [WMI] USB Detect 예제

* C# WMI 를 이용한 USB 연결 및 연결해제 예제... - WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 using System.Management - 사용한 컨트롤 : ListView 1개, Button 2개 전체 소..

kdsoft-zeros.tistory.com

 

반응형
반응형

* C# WMI 를 이용한 USB 연결 및 연결해제 예제...

- WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 using System.Management


Main

 

- 사용한 컨트롤 : ListView 1개, Button 2개

 

전체 소스 코드

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_WMI_USB_Detect
{
    public partial class Form1 : Form
    {
        ManagementEventWatcher mewWatcher;
        string strUSBDriveName;
        string strUSBDriveLetter;
        int iCount = 0;

        public Form1()
        {
            InitializeComponent();
            //크로스 스레드 
            CheckForIllegalCrossThreadCalls = false;
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            if (mewWatcher != null)
            {
                mewWatcher.Stop();
                mewWatcher.Dispose();
            }

        }

        void mewWatcher_EventArrived(object sender, System.Management.EventArrivedEventArgs e)
        {
            ManagementBaseObject mbo1;
            ManagementBaseObject mbo2;

            mbo1 = e.NewEvent as ManagementBaseObject;
            mbo2 = mbo1["TargetInstance"] as ManagementBaseObject;

            switch (mbo1.ClassPath.ClassName)
            {
                case "__InstanceCreationEvent":
                {
                    if (mbo2["InterfaceType"].ToString() == "USB")
                    {
                        strUSBDriveName = mbo2["Caption"].ToString();
                        strUSBDriveLetter = mbo2["Name"].ToString();

                        ListViewItem lvi = new ListViewItem();
                        lvi.Text = (iCount + 1).ToString();
                        lvi.SubItems.Add(strUSBDriveName + " : " + strUSBDriveLetter + " 연결 되었습니다.");

                        listView1.Items.Add(lvi);
                        iCount += 1;
                    }

                    break;
                }
                case "__InstanceDeletionEvent":
                {
                    if (mbo2["InterfaceType"].ToString() == "USB")
                    {
                        if (mbo2["Caption"].ToString() == strUSBDriveName)
                        {
                            ListViewItem lvi = new ListViewItem();
                            lvi.Text = (iCount + 1).ToString();
                            lvi.SubItems.Add(strUSBDriveName + " : " + strUSBDriveLetter + " 해제 되었습니다.");

                            listView1.Items.Add(lvi);
                            iCount += 1;

                            strUSBDriveLetter = "";
                            strUSBDriveName = "";
                        }
                    }

                    break;
                }
            }

            this.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //start
            //ManageMentEventWatcher 가 null 이면 생성...
            if (mewWatcher == null) mewWatcher = new ManagementEventWatcher();

            WqlEventQuery weQuery = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " + "WHERE TargetInstance ISA 'Win32_DiskDrive'");
            mewWatcher.Query = weQuery;
            //Event Create
            mewWatcher.EventArrived += new EventArrivedEventHandler(mewWatcher_EventArrived);
            mewWatcher.Start();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //stop
            mewWatcher.Stop();
            mewWatcher.Dispose();
        }
    }
}

 

위 구문에서 보듯이 CheckfoCheckForIllegalCrossThreadCalls 를 사용 (크로스 스레드 예제 https://kdsoft-zeros.tistory.com/22 참조)

 

- 아래 클래스에 붉은색 표시 -> 소스코드에 사용한 필드 !!

 

[Dynamic, Provider("CIMWin32"), UUID("{8502C4B2-5FBB-11D2-AAC1-006008C78BC7}"), AMENDMENT]
class Win32_DiskDrive : CIM_DiskDrive
{
  uint16   Availability;
  uint32   BytesPerSector;
  uint16   Capabilities[];
  string   CapabilityDescriptions[];
  string   Caption;
  string   CompressionMethod;
  uint32   ConfigManagerErrorCode;
  boolean  ConfigManagerUserConfig;
  string   CreationClassName;
  uint64   DefaultBlockSize;
  string   Description;
  string   DeviceID;
  boolean  ErrorCleared;
  string   ErrorDescription;
  string   ErrorMethodology;
  string   FirmwareRevision;
  uint32   Index;
  datetime InstallDate;
  string   InterfaceType;
  uint32   LastErrorCode;
  string   Manufacturer;
  uint64   MaxBlockSize;
  uint64   MaxMediaSize;
  boolean  MediaLoaded;
  string   MediaType;
  uint64   MinBlockSize;
  string   Model;
  string   Name;
  boolean  NeedsCleaning;
  uint32   NumberOfMediaSupported;
  uint32   Partitions;
  string   PNPDeviceID;
  uint16   PowerManagementCapabilities[];
  boolean  PowerManagementSupported;
  uint32   SCSIBus;
  uint16   SCSILogicalUnit;
  uint16   SCSIPort;
  uint16   SCSITargetId;
  uint32   SectorsPerTrack;
  string   SerialNumber;
  uint32   Signature;
  uint64   Size;
  string   Status;
  uint16   StatusInfo;
  string   SystemCreationClassName;
  string   SystemName;
  uint64   TotalCylinders;
  uint32   TotalHeads;
  uint64   TotalSectors;
  uint64   TotalTracks;
  uint32   TracksPerCylinder;
};

 

 

*예제 결과

 

- 연결된 모습

 

-연결 해제된 모습

 

* 참조 (마이크로소프트)

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-diskdrive

 

Win32_DiskDrive class - Win32 apps

Win32_DiskDrive class In this article --> The Win32_DiskDrive WMI class represents a physical disk drive as seen by a computer running the Windows operating system. The following syntax is simplified from Managed Object Format (MOF) code and includes all o

docs.microsoft.com

 

https://kdsoft-zeros.tistory.com/182

 

[VBNET] [WMI] USB Detect 예제

* VBNET WMI 를 이용한 USB 연결 및 연결해제 예제... - WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 Imports System.Management - 사용한 컨트롤 : ListView 1개, Button 2개 전..

kdsoft-zeros.tistory.com

 

반응형
반응형

* VBNET WMI 를 이용한 CPU 클럭 속도 가져오기 예제...

 

- WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 imports System.Management

 

- 사용 컨트롤 : Label 1개, Button 1개

 

전체 소스 코드

Form1.vb

 


Imports System.Management

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

        Using mo As ManagementObject = New ManagementObject("Win32_Processor.DeviceID='CPU0'")

            Dim iSpeed As UInteger = CType(mo("CurrentClockSpeed"), UInteger)
            lbl0.Text = iSpeed.ToString()

        End Using

    End Sub

End Class

 

 

*예제 결과

 

 

단위는 메가 헤르츠 이며 내컴퓨터 속성에서 보듯이 값이 똑같다는 걸 볼 수 있습니다.

 

*참조 문서 (마이크로소프트)

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor



https://kdsoft-zeros.tistory.com/179



 

[C#] [WMI] CPU 클럭 속도 (CurrentClockSpeed)

* C# WMI 를 이용한 CPU 클럭 속도 가져오기 예제... - WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 using System.Management - 사용 컨트롤 : Label 1개, Button 1개 전체 소스..

kdsoft-zeros.tistory.com

 

반응형
반응형

* C# WMI 를 이용한 CPU 클럭 속도 가져오기 예제...

 

- 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();
            }

        }
    }

}

 

*예제 결과

 

 

단위는 메가 헤르츠 이며 내컴퓨터 속성에서 보듯이 값이 똑같다는 걸 볼 수 있습니다.

 

*참조 문서 (마이크로소프트)

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor

 

Win32_Processor class - Win32 apps

Win32_Processor class In this article --> The Win32_Processor WMI class represents a device that can interpret a sequence of instructions on a computer running on a Windows operating system. The following syntax is simplified from Managed Object Format (MO

docs.microsoft.com

 

https://kdsoft-zeros.tistory.com/180

 

[VBNET] [WMI] CPU 클럭 속도 (CurrentClockSpeed)

* VBNET WMI 를 이용한 CPU 클럭 속도 가져오기 예제... - WMI 를 사용하기 위해 참조 -> System.Management dll 을 추가 -> 소스 코드 imports System.Management - 사용 컨트롤 : Label 1개, Button 1개 전체..

kdsoft-zeros.tistory.com

 

반응형
반응형

* VBNET 파일 비교 (File Compare) 예제...

 

Main

- 사용한 컨트롤 : Label 5개 , Button 3개 , GroupBox 1개

전체 소스 코드

Form1.vb

 

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

 

 

*예제 결과

 

- 길이 및 읽은 바이트 수가 같으면...

 

 

- 파일 위치 및 이름이 같으면...

 

 

- 파일 길이 및 이름, 읽은 바이트 수가 다르면...

 

 

https://kdsoft-zeros.tistory.com/177

 

[C#] 파일 비교 (File Compare)

* C# 파일 비교 (File Compare) 예제... - 사용한 컨트롤 : Label 5개 , Button 3개 , GroupBox 1개 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; us..

kdsoft-zeros.tistory.com

 

반응형
반응형

* C# 파일 비교  (File Compare) 예제...

 

Main

- 사용한 컨트롤 : Label 5개 , Button 3개 , GroupBox 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;

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 = "같은 파일 입니다.";
            }

        }
    }
}

 

 

*예제 결과

 

- 길이 및 읽은 바이트 수가 같으면...

 

 

- 파일 위치 및 이름이 같으면...

 

 

- 파일 길이 및 이름, 읽은 바이트 수가 다르면...

 

 

https://kdsoft-zeros.tistory.com/178

 

[VBNET] 파일 비교 (File Compare)

* VBNET 파일 비교 (File Compare) 예제... - 사용한 컨트롤 : Label 5개 , Button 3개 , GroupBox 1개 전체 소스 코드 Form1.vb Public Class Form1 Private Sub button1_Click(ByVal sender As System.Object,..

kdsoft-zeros.tistory.com

 

반응형

+ Recent posts