Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click 'Folder Open Dim fbd As FolderBrowserDialog = New FolderBrowserDialog
Try Dim strAdminUserName As String = Environment.UserName Dim ds As System.Security.AccessControl.DirectorySecurity = System.IO.Directory.GetAccessControl(label1.Text) Dim fsa As System.Security.AccessControl.FileSystemAccessRule = New System.Security.AccessControl.FileSystemAccessRule(strAdminUserName, _ Security.AccessControl.FileSystemRights.FullControl, _ Security.AccessControl.AccessControlType.Deny)
Dim strMsg As String = "" Dim ws As Xml.XmlWriterSettings = New Xml.XmlWriterSettings() ws.Indent = True Dim bCheck As Boolean = False Dim iCount As Integer = 0
* DateTime Class 를 이용한 현재 선택된 달의 마지막 날짜 및 요일 구하기 예제...
메인화면
전체 소스코드
Form1.vb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'C# 과 같은 DateTime Class 이용
numYear.Value = DateTime.Now.Year
numMonth.Value = DateTime.Now.Month
'VB6 처럼 사용
'numYear.Value = Date.Now.Year
'numMonth.Value = Date.Now.Month
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'날짜 타입으로 변환 하기...
Dim dtTmp As DateTime = DateTime.Parse(numYear.Value.ToString() + "-" + numMonth.Value.ToString() + "-01")
'현재 선택된 달 +1 하기...
Dim dt As DateTime = dtTmp.AddMonths(1)
'+1 된 달에서 하루 빼기...
dt = dt.AddDays(-1)
label4.Text = "선택된 년월: " + numYear.Value.ToString() + "-" + numMonth.Value.ToString()
label5.Text = "마지막 일: " + dt.ToString("dd")
label6.Text = "마지막 요일: " + GetDayOfWeek(dt)
End Sub
Private Function GetDayOfWeek(ByVal dt As DateTime) As String
Dim strDay As String = ""
Select Case dt.DayOfWeek
Case DayOfWeek.Monday
strDay = "월요일"
Case DayOfWeek.Tuesday
strDay = "화요일"
Case DayOfWeek.Wednesday
strDay = "수요일"
Case DayOfWeek.Thursday
strDay = "목요일"
Case DayOfWeek.Friday
strDay = "금요일"
Case DayOfWeek.Saturday
strDay = "토요일"
Case DayOfWeek.Sunday
strDay = "일요일"
End Select
Return strDay
End Function
End Class
* 사용자 정의 함수 GetDayOfWeek => 요일 값을 문자열로 변환 하여 리턴 하는 함수
Public Class Form1
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'int 형으로 변환
Dim iReturnValue As Integer = IsInt(textBox1.Text)
If iReturnValue = 0 Then
label1.Text = "int 형 변환으로 실패..."
Return
End If
label1.Text = "int 형 변환 성공..."
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
'Double 형으로 변환
Dim dbReturnValue As Double = IsDouble(textBox1.Text)
If dbReturnValue = 0 Then
label1.Text = "Double 형 변환으로 실패..."
Return
End If
label1.Text = "Double 형 변환 성공..."
End Sub
Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
'String 값 null 체크 및 빈 값 체크
If IsNullString(textBox1.Text) Then
label1.Text = "string 값이 Null 또는 빈 값입니다..."
Return
End If
label1.Text = "string 값이 정상적입니다..."
End Sub
Private Function IsInt(ByVal ob As Object) As Integer
If ob Is Nothing Then Return 0
Dim i As Integer
'int 형 변환
Dim b As Boolean = Integer.TryParse(ob.ToString(), i)
If Not b Then Return 0
Return i
End Function
Private Function IsDouble(ByVal ob As Object) As Double
If ob Is Nothing Then Return 0
Dim db As Double
'double 형 변환
Dim b As Boolean = Double.TryParse(ob.ToString(), db)
If Not b Then Return 0
Return db
End Function
Private Function IsNullString(ByVal strTmp As String) As Boolean
Return String.IsNullOrEmpty(strTmp)
End Function
End Class
위 그림과 같이 int.TryParse 사용으로 오류없이 자연스럽게 형 변환 하는 모습입니다.
물론 Convert.ToInt32 () 로 가능 하며, int.Parse 로도 가능 하지만 예기치 못한 string 값에
숫자가 아닌 다른 문자열이 들어 가게 된다면... try~ catch~ 문이 없다면 오류를 내면서 프로그램
이 비정상적으로 종료 되는 걸 볼 수 있습니다.
int 형으로 변환 실패 된 그림 예 입니다. 만약 int.Parse 와 Convert.ToInt32 로 변환 하였다면
아래의 그림과 같이 오류 메시지가 뜨게 됩니다.
Double 형 변환 또한 int 형 변환 설명 드렸듯이 같습니다.
형 변환 성공형 변환 실패
마지막으로 string.IsNullOrEmpty() 함수로 string 문자열이 빈 값 인지 또는 null 값 인지 체크 하는 예 입니다.
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
Public Class Form1
Dim pcEXE As System.Diagnostics.Process
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'파일 열기...
Dim ofd As OpenFileDialog = New OpenFileDialog()
'EXE 파일만 열기...
ofd.Filter = "EXE File (*.exe) | *.exe"
If (ofd.ShowDialog() = Windows.Forms.DialogResult.OK) Then
label1.Text = ofd.FileName
End If
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
'응용 프로그램 실행 시키기...
'해당 파일이 존재 하지 않으면...
If Not System.IO.File.Exists(label1.Text) Then
Return
End If
'다른 응용 프로그램 실행 시키기...
pcEXE = System.Diagnostics.Process.Start(label1.Text)
End Sub
Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
'응용 프로그램 죽이기...
If pcEXE Is Nothing Then Return
'죽이기...
pcEXE.Kill()
End Sub
End Class