반응형
* 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
https://kdsoft-zeros.tistory.com/72
반응형
'VB.NET Programming' 카테고리의 다른 글
[VBNET] DateTime 클래스 : 현재 선택된 달의 마지막 요일 및 날짜 구하기 (0) | 2019.11.10 |
---|---|
[VBNET] string 을 int 및 double 형으로 변환 하기, string null 체크 (0) | 2019.11.06 |
[VBNET] 다른 응용 프로그램 실행 및 종료 (0) | 2019.10.29 |
[VBNET] File 사용 가능 여부 체크 (0) | 2019.10.26 |
[VBNET] XML File Write & Read 예제 (0) | 2019.10.24 |