30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

760 Files and Streams Chapter 17<br />

46 ' handle exception if StreamReader is unavailable<br />

47 Catch exceptionCatch As IOException<br />

48<br />

49 ' display error<br />

50 MessageBox.Show("FILE ERROR", "FILE ERROR", _<br />

51 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Error)<br />

52<br />

53 End Try<br />

54<br />

55 ' determine whether fileName is a direc<strong>to</strong>ry<br />

56 ElseIf Direc<strong>to</strong>ry.Exists(fileName) Then<br />

57<br />

58 Dim direc<strong>to</strong>ryList As String() ' array for direc<strong>to</strong>ries<br />

59 Dim i As Integer<br />

60<br />

61 ' get direc<strong>to</strong>ry's creation date, modification date, etc<br />

62 txtOutput.Text = GetInformation(fileName)<br />

63<br />

64 ' obtain direc<strong>to</strong>ry list of specified direc<strong>to</strong>ry<br />

65 direc<strong>to</strong>ryList = Direc<strong>to</strong>ry.GetDirec<strong>to</strong>ries(fileName)<br />

66<br />

67 txtOutput.Text &= vbCrLf & vbCrLf & _<br />

68 "Direc<strong>to</strong>ry contents:" & vbCrLf<br />

69<br />

70 ' output direc<strong>to</strong>ryList contents<br />

71 For i = 0 To direc<strong>to</strong>ryList.Length - 1<br />

72 txtOutput.Text &= direc<strong>to</strong>ryList(i) & vbCrLf<br />

73 Next<br />

74<br />

75 ' notify user that neither file nor direc<strong>to</strong>ry exists<br />

76 Else<br />

77 MessageBox.Show(txtInput.Text & " does not exist", _<br />

78 "FILE ERROR", MessageBoxBut<strong>to</strong>ns.OK, _<br />

79 MessageBoxIcon.Error)<br />

80 End If<br />

81<br />

82 End If ' determine whether user pressed Enter key<br />

83<br />

84 End Sub ' txtInput_KeyDown<br />

85<br />

86 ' get information on file or direc<strong>to</strong>ry<br />

87 Private Function GetInformation(ByRef fileName As String) _<br />

88 As String<br />

89<br />

90 Dim information As String<br />

91<br />

92 ' output that file or direc<strong>to</strong>ry exists<br />

93 information = fileName & " exists" & vbCrLf & vbCrLf<br />

94<br />

95 ' output when file or direc<strong>to</strong>ry was created<br />

96 information &= "Created : " & _<br />

97 File.GetCreationTime(fileName) & vbCrLf<br />

98<br />

Fig. 17.5 FrmFileTest class tests classes File and Direc<strong>to</strong>ry (part 2 of 3).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!