-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOutputFile.vb
More file actions
executable file
·123 lines (99 loc) · 3.9 KB
/
OutputFile.vb
File metadata and controls
executable file
·123 lines (99 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Imports System.IO
Imports System.Text.RegularExpressions
Public Class OutputFile
Private _oWriteStream As FileStream
Private _oFileInfo As FileInfo
Private _oStreamWriter As StreamWriter
Private _oLines As Collection
Private _sFullFilename As String
Private _bOverwriteExisting As Boolean
Private Shared _oFilesCreated As Collection
Public Sub New(ByVal sFullFilename As String, Optional ByVal bOverwriteExisting As Boolean = True)
MyBase.New()
_sFullFilename = sFullFilename
_bOverwriteExisting = bOverwriteExisting
If _oFilesCreated Is Nothing Then
_oFilesCreated = New Collection
End If
initializeFileIO()
End Sub
Public Sub Add(Optional ByVal sLineText As String = "", Optional ByVal SuppressEOL As Boolean = False)
Dim sMassagedLine As String = sLineText
_oLines.Add(sMassagedLine)
End Sub
Public Sub AddLine(Optional ByVal sLineText As String = "", Optional ByVal SuppressEOL As Boolean = False)
Dim sMassagedLine As String = sLineText & vbCrLf
_oLines.Add(sMassagedLine)
End Sub
Public Sub Close()
Dim sLine As String
On Error Resume Next ' just ignore any errors as the file closes
If _oStreamWriter IsNot Nothing Then
For Each sLine In _oLines
_oStreamWriter.WriteLine(sLine)
Next
_oStreamWriter.Close()
_oStreamWriter = Nothing
End If
If _oWriteStream IsNot Nothing Then
_oWriteStream.Close()
_oWriteStream = Nothing
End If
End Sub
Public Overrides Function ToString() As String
Dim sString As String = ""
Dim sLine As String
For Each sLine In _oLines
sString &= sLine
Next
Return sString
End Function
Private Sub initializeFileIO()
Try
OutputFile.ClearFilesCreated()
_oLines = New Collection
_oFileInfo = New FileInfo(_sFullFilename)
With _oFileInfo
If .Exists Then
If Not _bOverwriteExisting Then
Throw New ApplicationException("File '" & _sFullFilename & "' already exists and may not be overwritten")
End If
_oFileInfo.Attributes = (_oFileInfo.Attributes Or System.IO.FileAttributes.ReadOnly) Xor System.IO.FileAttributes.ReadOnly ' make file writable
_oFileInfo.Delete()
End If
_oWriteStream = _oFileInfo.Create
_oStreamWriter = New StreamWriter(_oWriteStream)
If Not _oFilesCreated.Contains(_oFileInfo.Name) Then
_oFilesCreated.Add(_oFileInfo.Name, _oFileInfo.FullName)
End If
End With
Catch ex As Exception
Dim oErrorHandler As New sjmErrorHandler(ex)
End Try
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
Me.Close()
End Sub
Public Function GetFiles(ByVal sfilenameSpec As String) As Collection
Dim sFilenames As New Collection
Dim oDirectoryFiles() As FileInfo = _oFileInfo.Directory.GetFiles(sfilenameSpec, IO.SearchOption.TopDirectoryOnly)
For Each oFileInfo As FileInfo In oDirectoryFiles
sFilenames.Add(oFileInfo.Name, oFileInfo.FullName)
Next
Return sFilenames
End Function
Public Shared Sub ClearFilesCreated()
_oFilesCreated = New Collection
End Sub
Public ReadOnly Property FileInfo() As FileInfo
Get
Return _oFileInfo
End Get
End Property
Public Shared ReadOnly Property FilesCreated() As Collection
Get
Return _oFilesCreated
End Get
End Property
End Class