반응형

* VBNET 응용 프로그램 재시작 예제...

 

메인 화면

전체 소스 코드

Form1.vb

 

Imports System.Runtime.InteropServices

Public Class Form1

#Region "INI File...관련"
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
                                ByVal lpKeyName As String, _
                                ByVal lpDefault As String, _
                                ByVal lpReturnedString As System.Text.StringBuilder, _
                                ByVal nSize As Integer, _
                                ByVal lpFileName As String) As Integer
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, _
                            ByVal lpKeyName As String, _
                            ByVal lpString As String, _
                            ByVal lpFileName As String) As Boolean
    End Function

    Public Shared Function SetINI(ByVal strAppName As String, _
                           ByVal strKey As String, _
                           ByVal strValue As String, _
                           ByVal strFilePath As String) As Boolean
        SetINI = WritePrivateProfileString(strAppName, strKey, strValue, strFilePath)
    End Function
    Public Shared Function GetINI(ByVal strAppName As String, _
                           ByVal strKey As String, _
                           ByVal strValue As String, _
                           ByVal strFilePath As String) As String
        Dim strbTmp As System.Text.StringBuilder = New System.Text.StringBuilder(255)

        GetPrivateProfileString(strAppName, strKey, strValue, strbTmp, strbTmp.Capacity, strFilePath)

        GetINI = strbTmp.ToString()

    End Function

    Public Shared Function Create_INIFile(ByVal strPath As String, _
                                   ByVal strFileName As String) As Boolean
        '해당 파일이 있으면 그냥 나가기...
        ' System.IO.File.Exists(strPath + "\" + strFileName) 요렇게 해도 됨.
        If Dir(strPath & "\" & strFileName) <> "" Then
            Exit Function
        End If

        '해당 폴더가 없다면 만들기...
        If Not System.IO.Directory.Exists(strPath) Then
            System.IO.Directory.CreateDirectory(strPath)
        End If

        Try
            Using sw As System.IO.StreamWriter = New System.IO.StreamWriter(strPath & "\" & strFileName, False)
                sw.WriteLine(vbCrLf)
                sw.Flush()
                sw.Close()
            End Using
        Catch ex As Exception

            Return False
        End Try

        Return True
    End Function

#End Region

    Dim strINIPath As String = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\")) + "\INI"
    Dim iCount As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Create_INIFile(strINIPath, "ReStart.ini")

        label2.Text = GetINI("Restart_Info", "restart", "0", strINIPath + "\ReStart.ini")
        iCount = Convert.ToInt32(label2.Text)

    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        '다시 시작 하기...
        iCount += 1
        SetINI("Restart_Info", "restart", iCount.ToString(), strINIPath + "\ReStart.ini")

        '프로그램 종료
        Application.Exit()

        '1초 뒤에...
        System.Threading.Thread.Sleep(1000)

        '1번째 방법...
        'Application.Restart()
        '2번째 방법...
        System.Diagnostics.Process.Start(Application.ExecutablePath)
    End Sub


End Class

 

 

* 예제 결과

 

 

 

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

 

[VBNET] INIFile Create & Read & Write Example

* INI 파일 예제 Form1.vb Imports System.Runtime.InteropServices Imports System.IO Imports System.Text Public Class Form1 '빌드되는 폴더 경로 가져오기... Dim strCheckFolder As String = Application.E..

kdsoft-zeros.tistory.com

 

 

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

 

[C#] 응용 프로그램 재시작 예제

* C# 프로그램 재시작 예제... 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using S..

kdsoft-zeros.tistory.com

 

반응형

+ Recent posts