/
FileSystem.vb
1517 lines (1276 loc) · 65.5 KB
/
FileSystem.vb
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
Imports System
Imports System.Diagnostics
Imports System.Diagnostics.CodeAnalysis
Imports System.Globalization
Imports System.IO
Imports System.Runtime.Versioning
Imports System.Text
Imports Microsoft.VisualBasic.CompilerServices
Imports Microsoft.VisualBasic.CompilerServices.ExceptionUtils
Imports Microsoft.VisualBasic.CompilerServices.IOUtils
Imports Microsoft.VisualBasic.CompilerServices.Utils
Namespace Microsoft.VisualBasic
Public Module FileSystem
Private Const ERROR_FILE_NOT_FOUND As Integer = 2
Private Const ERROR_FILE_EXISTS As Integer = 80
Private Const ERROR_ALREADY_EXISTS As Integer = 183
Private Const ERROR_INVALID_ACCESS As Integer = 12
Private Const ERROR_NOT_SAME_DEVICE As Integer = 17
Friend Enum vbFileType
vbPrintFile = 0
vbWriteFile = 1
End Enum
'FILESYSTEM function vars
Friend Const FIRST_LOCAL_CHANNEL As Integer = 1
Friend Const LAST_LOCAL_CHANNEL As Integer = 255
Friend Const sTimeFormat As String = "T"
Friend Const sDateFormat As String = "d"
Friend Const sDateTimeFormat As String = "F"
Private Const FileSystemTargetObjectTrimmerMessage As String = "The target object type could not be statically analyzed and may be trimmed"
Private Const FileSystemOriginObjectTrimmerMessage As String = "The origin object type could not be statically analyzed and may be trimmed"
Friend ReadOnly m_WriteDateFormatInfo As DateTimeFormatInfo = InitializeWriteDateFormatInfo() ' Call static initializer due to FxCop InitializeReferenceTypeStaticFieldsInline.
Private Function InitializeWriteDateFormatInfo() As DateTimeFormatInfo
Dim dfi As New DateTimeFormatInfo
dfi.DateSeparator = "-"
dfi.ShortDatePattern = "\#yyyy-MM-dd\#"
dfi.LongTimePattern = "\#HH:mm:ss\#"
dfi.FullDateTimePattern = "\#yyyy-MM-dd HH:mm:ss\#"
Return dfi
End Function
'============================================================================
' Directory/drive functions.
'============================================================================
Public Sub ChDir(ByVal Path As String)
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
Path = RTrim(Path) 'VB6 accepted things like "\ ", so need to trim the trailing spaces
If (Path Is Nothing) OrElse (Path.Length = 0) Then
Throw VbMakeException(New ArgumentException(SR.Argument_PathNullOrEmpty), vbErrors.BadFileNameOrNumber)
End If
' Do this since System.IO.Directory does not accept "\"
If Path = "\" Then
Path = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory())
End If
Try
System.IO.Directory.SetCurrentDirectory(Path)
Catch ex As System.IO.FileNotFoundException
Throw VbMakeException(New FileNotFoundException(SR.Format(SR.FileSystem_PathNotFound1, Path)), vbErrors.PathNotFound)
End Try
End Sub
<SupportedOSPlatform("windows")>
Public Sub ChDrive(ByVal Drive As Char)
Drive = System.Char.ToUpperInvariant(Drive)
If (Drive < chLetterA) OrElse (Drive > chLetterZ) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Drive"))
End If
If Not UnsafeValidDrive(Drive) Then
Throw VbMakeException(New IOException(SR.Format(SR.FileSystem_DriveNotFound1, CStr(Drive))), vbErrors.DevUnavailable)
End If
IO.Directory.SetCurrentDirectory(Drive & Path.VolumeSeparatorChar)
End Sub
<SupportedOSPlatform("windows")>
Public Sub ChDrive(ByVal Drive As String)
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
If Drive Is Nothing OrElse Drive.Length = 0 Then
Exit Sub
End If
ChDrive(Drive.Chars(0))
End Sub
Public Function CurDir() As String
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
Return Directory.GetCurrentDirectory()
End Function
<SupportedOSPlatform("windows")>
Public Function CurDir(ByVal Drive As Char) As String
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
Drive = System.Char.ToUpperInvariant(Drive)
If (Drive < chLetterA OrElse Drive > chLetterZ) Then
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Drive")), vbErrors.DevUnavailable)
End If
'GetFullPath("x:.") will return the full directory path
Dim CurrentPath As String = Path.GetFullPath(Drive & Path.VolumeSeparatorChar & ".")
If Not UnsafeValidDrive(Drive) Then
Throw VbMakeException(New IOException(SR.Format(SR.FileSystem_DriveNotFound1, CStr(Drive))), vbErrors.DevUnavailable)
End If
Return CurrentPath
End Function
Public Function Dir() As String
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
Return FindNextFile(System.Reflection.Assembly.GetCallingAssembly())
End Function
<SupportedOSPlatform("windows")>
<ResourceExposure(ResourceScope.None)>
<ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)>
Public Function Dir(ByVal PathName As String, Optional ByVal Attributes As FileAttribute = FileAttribute.Normal) As String
'VB's FileAttribute is different than System.IO.FileAttributes:
' VB URT
'Normal 0 128
'ReadOnly 1 1
'Hidden 2 2
'System 4 4
'Volume 8 --
'Directory 16 16
'Archive 32 32
'Device -- 64
'Temporary -- 256
'SparseFile -- 512
'ReparsePoint -- 1024
'Compressed -- 2048
'Offline -- 4096
'NotContentIndexed -- 8192
'Encrypted -- 16384
'Note: Do NOT throw if pathName = "". That's legal for this function - returns the first file found.
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
If Attributes = FileAttribute.Volume Then
#If TARGET_WINDOWS Then
Dim Result As Integer
Dim VolumeName As StringBuilder = New StringBuilder(256)
Dim RootName As String = Nothing
If (PathName.Length > 0) Then
RootName = Path.GetPathRoot(PathName)
'Add a backslash if one isn't there. This is required by GetVolumeInformation
If RootName.Chars(RootName.Length - 1) <> Path.DirectorySeparatorChar Then
RootName &= Path.DirectorySeparatorChar
End If
End If
Result = NativeMethods.GetVolumeInformation(RootName, VolumeName, 256, 0, 0, 0, Nothing, 0)
If Result <> 0 Then
Return VolumeName.ToString
Else
Return ""
End If
#Else
Throw New PlatformNotSupportedException()
#End If
Else
'Dir function always returns files with Normal attribute in addition to others specified.
Dim URTAttributes As System.IO.FileAttributes = CType(Attributes, FileAttributes) Or FileAttributes.Normal
Return FindFirstFile(System.Reflection.Assembly.GetCallingAssembly(), PathName, URTAttributes)
End If
End Function
Public Sub MkDir(ByVal Path As String)
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
If Path Is Nothing OrElse Path.Length = 0 Then
Throw VbMakeException(New ArgumentException(SR.Argument_PathNullOrEmpty), vbErrors.BadFileNameOrNumber)
End If
If Directory.Exists(Path) Then
Throw VbMakeException(vbErrors.PathFileAccess)
Else
Directory.CreateDirectory(Path)
End If
End Sub
Public Sub RmDir(ByVal Path As String)
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
'If null or empty directory, give error
If Path Is Nothing OrElse Path.Length = 0 Then
Throw VbMakeException(New ArgumentException(SR.Argument_PathNullOrEmpty), vbErrors.BadFileNameOrNumber)
End If
Try
Directory.Delete(Path)
Catch e1 As DirectoryNotFoundException
Throw VbMakeException(e1, vbErrors.PathNotFound)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch e2 As Exception
Throw VbMakeException(e2, vbErrors.PathFileAccess)
End Try
End Sub
'============================================================================
' File functions.
'============================================================================
Private Function PathContainsWildcards(ByVal Path As String) As Boolean
If Path Is Nothing Then
Return False
End If
If (Path.Contains("*"c)) Then
Return True
End If
If (Path.Contains("?"c)) Then
Return True
End If
Return False
End Function
Public Sub FileCopy(ByVal Source As String, ByVal Destination As String)
If (Source Is Nothing) OrElse (Source.Length = 0) Then
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_PathNullOrEmpty1, "Source")), vbErrors.BadFileNameOrNumber)
End If
If (Destination Is Nothing) OrElse (Destination.Length = 0) Then
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_PathNullOrEmpty1, "Destination")), vbErrors.BadFileNameOrNumber)
End If
' Error if wildcard characters in name
If PathContainsWildcards(Source) Then
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Source")), vbErrors.BadFileNameOrNumber)
End If
If PathContainsWildcards(Destination) Then
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Destination")), vbErrors.BadFileNameOrNumber)
End If
Dim oAssemblyData As AssemblyData = ProjectData.GetProjectData().GetAssemblyData(System.Reflection.Assembly.GetCallingAssembly())
If CheckFileOpen(oAssemblyData, Destination, OpenModeTypes.Output) Then
Throw VbMakeException(New IOException(SR.Format(SR.FileSystem_FileAlreadyOpen1, Destination)), vbErrors.FileAlreadyOpen)
End If
If CheckFileOpen(oAssemblyData, Source, OpenModeTypes.Input) Then
Throw VbMakeException(New IOException(SR.Format(SR.FileSystem_FileAlreadyOpen1, Source)), vbErrors.FileAlreadyOpen)
End If
Try
File.Copy(Source, Destination, True)
'VB6 did not copy file attributes, so we must be backwards compatible
File.SetAttributes(Destination, FileAttributes.Archive)
'Need to emulate vb6 error codes as much as possible
Catch ex As FileNotFoundException
Throw VbMakeException(ex, vbErrors.FileNotFound)
Catch ex As IOException
Throw VbMakeException(ex, vbErrors.FileAlreadyOpen)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Function FileDateTime(ByVal PathName As String) As DateTime
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
If PathContainsWildcards(PathName) Then
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "PathName")), vbErrors.BadFileNameOrNumber)
End If
If File.Exists(PathName) Then
Return (New FileInfo(PathName)).LastWriteTime
End If
Throw New FileNotFoundException(SR.Format(SR.FileSystem_FileNotFound1, PathName))
End Function
Public Function FileLen(ByVal PathName As String) As Long
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
If File.Exists(PathName) Then
Return (New FileInfo(PathName)).Length
End If
Throw New FileNotFoundException(SR.Format(SR.FileSystem_FileNotFound1, PathName))
End Function
Public Function GetAttr(ByVal PathName As String) As FileAttribute
'VB's FileAttribute is different than System.IO.FileAttributes:
' VB URT
'Normal 0 128
'ReadOnly 1 1
'Hidden 2 2
'System 4 4
'Volume 8 --
'Directory 16 16
'Archive 32 32
'Device -- 64
'Temporary -- 256
'SparseFile -- 512
'ReparsePoint -- 1024
'Compressed -- 2048
'Offline -- 4096
'NotContentIndexed -- 8192
'Encrypted -- 16384
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
Dim WildCards() As Char = {"*"c, "?"c}
If PathName.IndexOfAny(WildCards) >= 0 Then
Throw VbMakeException(vbErrors.BadFileNameOrNumber)
End If
Dim f As New FileInfo(PathName)
If f.Exists Then
'Mask off any attributes that VB doesn't define.
Return CType(f.Attributes And &H3F, FileAttribute)
Else
Dim d As New DirectoryInfo(PathName)
If d.Exists Then
'Mask off any attributes that VB doesn't define.
Return CType(d.Attributes And &H3F, FileAttribute)
End If
End If
If Path.GetFileName(PathName).Length = 0 Then
Throw VbMakeException(vbErrors.BadFileNameOrNumber)
Else
Throw New FileNotFoundException(SR.Format(SR.FileSystem_FileNotFound1, PathName))
End If
End Function
Public Sub Kill(ByVal PathName As String)
Debug.Assert(Not System.Reflection.Assembly.GetCallingAssembly() Is Utils.VBRuntimeAssembly,
"Methods in Microsoft.VisualBasic should not call FileSystem public method.")
Dim dir As DirectoryInfo
Dim DirName As String
Dim FileName As String
Dim files() As FileInfo
Dim file As FileInfo
Dim DeleteCount As Integer
Dim i As Integer
DirName = Path.GetDirectoryName(PathName)
If (DirName Is Nothing) OrElse (DirName.Length = 0) Then
DirName = Environment.CurrentDirectory
FileName = PathName
Else
FileName = Path.GetFileName(PathName)
End If
dir = New DirectoryInfo(DirName)
files = dir.GetFiles(FileName)
DirName = DirName & Path.PathSeparator
If (Not files Is Nothing) Then
For i = 0 To files.GetUpperBound(0)
file = files(i)
'Don't delete hidden or system files
If (file.Attributes And (FileAttribute.Hidden Or FileAttribute.System)) = 0 Then
FileName = file.FullName
' error if file is presently open
Dim oAssemblyData As AssemblyData = ProjectData.GetProjectData().GetAssemblyData(System.Reflection.Assembly.GetCallingAssembly())
If CheckFileOpen(oAssemblyData, FileName, OpenModeTypes.Any) Then
Throw VbMakeException(New IOException(SR.Format(SR.FileSystem_FileAlreadyOpen1, FileName)), vbErrors.FileAlreadyOpen)
End If
Try
IO.File.Delete(FileName)
DeleteCount += 1
Catch ex As IOException
'Need to emulate vb6 error codes as much as possible
Throw VbMakeException(ex, vbErrors.FileAlreadyOpen)
Catch ex As Exception
Throw ex
End Try
End If
Next i
End If
If DeleteCount = 0 Then
Throw New IO.FileNotFoundException(SR.Format(SR.KILL_NoFilesFound1, PathName))
End If
End Sub
Public Sub SetAttr(ByVal PathName As String, ByVal Attributes As FileAttribute)
'VB's FileAttribute is different than System.IO.FileAttributes:
' VB URT
'Normal 0 128
'ReadOnly 1 1
'Hidden 2 2
'System 4 4
'Volume 8 --
'Directory 16 16
'Archive 32 32
'Device -- 64
'Temporary -- 256
'SparseFile -- 512
'ReparsePoint -- 1024
'Compressed -- 2048
'Offline -- 4096
'NotContentIndexed -- 8192
'Encrypted -- 16384
'Check pathname for errors and if file is open for any mode except sequential input
If (PathName Is Nothing) OrElse (PathName.Length = 0) Then
Throw VbMakeException(New ArgumentException(SR.Argument_PathNullOrEmpty), vbErrors.BadFileNameOrNumber)
End If
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
Dim oAssemblyData As AssemblyData = ProjectData.GetProjectData().GetAssemblyData(assem)
VB6CheckPathname(oAssemblyData, PathName, OpenMode.Input)
'Only allow _A_RDONLY(1), _A_HIDDEN(2), _A_SYSTEM(4), _A_ARCH(20)
If ((Attributes Or &H27S) <> &H27S) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Attributes"))
End If
'Dir function always returns files with Normal attribute in addition to others specified.
Dim URTAttributes As System.IO.FileAttributes = CType(Attributes, FileAttributes)
System.IO.File.SetAttributes(PathName, URTAttributes)
End Sub
'IMPORTANT: This call provides sensitive information whether a device exists and should be used with extreme care
Private Function UnsafeValidDrive(ByVal cDrive As Char) As Boolean 'Return of True means not a valid drive
#If TARGET_WINDOWS Then
Dim iDrive As Integer = AscW(cDrive) - AscW(chLetterA)
Return (CLng(UnsafeNativeMethods.GetLogicalDrives()) And CLng(&H2 ^ iDrive)) <> 0
#Else
Throw New PlatformNotSupportedException()
#End If
End Function
'*****************************************
' FileSystem APIs
'*****************************************
Private Sub ValidateAccess(ByVal Access As OpenAccess)
If Access <> OpenAccess.Default AndAlso
Access <> OpenAccess.Read AndAlso
Access <> OpenAccess.ReadWrite AndAlso
Access <> OpenAccess.Write Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Access"))
End If
End Sub
Private Sub ValidateShare(ByVal Share As OpenShare)
If Share <> OpenShare.Default AndAlso
Share <> OpenShare.Shared AndAlso
Share <> OpenShare.LockRead AndAlso
Share <> OpenShare.LockReadWrite AndAlso
Share <> OpenShare.LockWrite Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Share"))
End If
End Sub
Private Sub ValidateMode(ByVal Mode As OpenMode)
If Mode <> OpenMode.Input AndAlso
Mode <> OpenMode.Output AndAlso
Mode <> OpenMode.Random AndAlso
Mode <> OpenMode.Append AndAlso
Mode <> OpenMode.Binary Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Mode"))
End If
End Sub
'============================================================================
' Initialization functions.
'============================================================================
'======================================
' Public APIs
'======================================
Public Sub FileOpen(
ByVal FileNumber As Integer,
ByVal FileName As String,
ByVal Mode As OpenMode,
Optional ByVal Access As OpenAccess = OpenAccess.Default,
Optional ByVal Share As OpenShare = OpenShare.Default,
Optional ByVal RecordLength As Integer = -1)
Try
ValidateMode(Mode)
ValidateAccess(Access)
ValidateShare(Share)
If (FileNumber < FIRST_LOCAL_CHANNEL OrElse FileNumber > LAST_LOCAL_CHANNEL) Then
Throw VbMakeException(vbErrors.BadFileNameOrNumber)
End If
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
vbIOOpenFile(assem, FileNumber, FileName, Mode, Access, Share, RecordLength)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileClose(ByVal ParamArray FileNumbers() As Integer)
'If the paramarray is empty, then all files get closed
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
Dim oAssemblyData As AssemblyData
oAssemblyData = ProjectData.GetProjectData().GetAssemblyData(assem)
If (FileNumbers Is Nothing) OrElse (FileNumbers.Length = 0) Then
CloseAllFiles(oAssemblyData)
Else
Dim Index As Integer
For Index = 0 To FileNumbers.GetUpperBound(0)
InternalCloseFile(oAssemblyData, FileNumbers(Index))
Next
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub ValidateGetPutRecordNumber(ByVal RecordNumber As Long)
If RecordNumber < 1 AndAlso RecordNumber <> -1 Then
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "RecordNumber")), vbErrors.BadRecordNum)
End If
End Sub
<RequiresUnreferencedCode(FileSystemTargetObjectTrimmerMessage)>
Public Sub FileGetObject(ByVal FileNumber As Integer, ByRef Value As Object, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).GetObject(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
<RequiresUnreferencedCode(FileSystemTargetObjectTrimmerMessage)>
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As ValueType, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
<RequiresUnreferencedCode(FileSystemTargetObjectTrimmerMessage)>
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As System.Array, Optional ByVal RecordNumber As Long = -1,
Optional ByVal ArrayIsDynamic As Boolean = False, Optional ByVal StringIsFixedLength As Boolean = False)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber, ArrayIsDynamic, StringIsFixedLength)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Boolean, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Byte, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Short, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Integer, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Long, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Char, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Single, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Double, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Decimal, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As String, Optional ByVal RecordNumber As Long = -1, Optional ByVal StringIsFixedLength As Boolean = False)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber, StringIsFixedLength)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FileGet(ByVal FileNumber As Integer, ByRef Value As Date, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Get(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
<RequiresUnreferencedCode(FileSystemOriginObjectTrimmerMessage)>
Public Sub FilePutObject(ByVal FileNumber As Integer, ByVal Value As Object, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).PutObject(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
<ObsoleteAttribute("FileSystem.FilePut has been deprecated. Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types.")>
Public Sub FilePut(ByVal FileNumber As Object, ByVal Value As Object, Optional ByVal RecordNumber As Object = -1)
Throw New ArgumentException(SR.UseFilePutObject)
End Sub
<RequiresUnreferencedCode(FileSystemOriginObjectTrimmerMessage)>
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As ValueType, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
<RequiresUnreferencedCode(FileSystemOriginObjectTrimmerMessage)>
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As System.Array, Optional ByVal RecordNumber As Long = -1,
Optional ByVal ArrayIsDynamic As Boolean = False, Optional ByVal StringIsFixedLength As Boolean = False)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber, ArrayIsDynamic, StringIsFixedLength)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Boolean, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Byte, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Short, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Integer, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Long, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Char, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Single, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Double, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Decimal, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As String, Optional ByVal RecordNumber As Long = -1, Optional ByVal StringIsFixedLength As Boolean = False)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber, StringIsFixedLength)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub FilePut(ByVal FileNumber As Integer, ByVal Value As Date, Optional ByVal RecordNumber As Long = -1)
Try
ValidateGetPutRecordNumber(RecordNumber)
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber, OpenModeTypes.Binary Or OpenModeTypes.Random).Put(Value, RecordNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Print(ByVal FileNumber As Integer, ByVal ParamArray Output() As Object)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Print(CType(Output, Object()))
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub PrintLine(ByVal FileNumber As Integer, ByVal ParamArray Output() As Object)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).PrintLine(CType(Output, Object()))
Catch ex As Exception
Throw ex
End Try
End Sub
<RequiresUnreferencedCode(FileSystemTargetObjectTrimmerMessage)>
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Object)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Boolean)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Byte)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Short)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Integer)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Long)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Char)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Single)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Double)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Decimal)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As String)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Input(ByVal FileNumber As Integer, ByRef Value As Date)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).Input(Value)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub [Write](ByVal FileNumber As Integer, ByVal ParamArray Output() As Object)
Try
Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly()
GetStream(assem, FileNumber).WriteHelper(Output)
Catch ex As Exception
Throw ex
End Try