Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 21

VB6 - FILES (Processing a random "file of records")

Write, read, re-write to a Direct (Random) file of records. See also READ, WRITE for sim le files.

Module: modFile
P!"lic T# e empRecord $ Define !ser-defined t# e. ID As Integer %ame As String & '( End T# e

9et 3*, Position, m#Record $ Read t5ird record. t1t%ame 4 m#Record.%ame 9oTo Adios Pro"lems. 7sg=o1 "=ad Inde1 - tr# Again" Adios. End S!" Private Sub cmdReadFile Clic!"# $ Declare ,aria"les. Dim m#Record As em Record Dim Position, totalRecords, "#tes0ile, "#tesRecord $ - en sam le file for random access. - en "c./TEST0I)E.t1t" 0or Random 2 As 3* )en 4 )en(m#Record) $find o!t 5ow man# records are t5ere in file 3* "#tes0ile 4 )-0(*) "#tesRecord 4 )en(m#Record) totalRecords 4 "#tes0ile < "#tesRecord $ read data (randoml#)from last to first rec. 0or Position 4 totalRecords To * Ste -* $ Read t5e sam le file !sing t5e 9et. 9et 3*, Position, m#Record $ Read t5e record $ S5ow t5e record into t5e list"o1 )ist*.AddItem m#Record.ID 8 " " 8 2 m#Record.%ame %e1t $+lose 3* $ +lose t5e acti,e file. End S!" Private Sub Form Load"# -n Error 9oTo Adios $delete an# re,io!s ,ersion of t5e file >ill "c./TEST0I)E.t1t" Adios. End S!"

Form: frmFile
Private Sub cmdClear Clic!"# )ist*.+lear End S!" Private Sub cmdMa!eFile Clic!"# $ Declare ,aria"les. Dim m#Record As em Record Dim Record%!m"er As Integer $ - en file for random access. - en "c./TEST0I)E.t1t" 0or Random 2 As 3* )en 4 )en(m#Record) $P!t some d!mm# data into t5e file 0or Record%!m"er 4 * To 6 $ )oo 6 times. m#Record.ID 4 Record%!m"er $ Define ID. m#Record.%ame 4 "7#%ame" 8 2 Record%!m"er $ +reate a string. $ Write record to file. P!t 3*, Record%!m"er, m#Record $ S5ow t5e record into t5e list"o1 )ist*.AddItem m#Record.ID 8 " " 8 2 m#Record.%ame %e1t Record%!m"er +lose 3* $ +lose file. End S!" Private Sub cmdRa$domRead Clic!"# -n Error 9oTo Pro"lems Dim m#Record As em Record Position 4 :al(t1tPosition) If Position ; )-0(*) < )en(m#Record) T5en 9oTo Pro"lems End If

VB%&E' ver(io$
empRecord Cla(( P!"lic Str!ct!re em Record $ Define !ser-defined t# e. Dim ID As S5ort )VBFi*edStri$+",-#. Dim %ame As String End Str!ct!re frmFile Cla(( "/I0# Pri,ate S!" cmdClear Clic!(=#:al e,entSender As S#stem.-"?ect, =#:al e,entArgs As S#stem.E,entArgs) @andles cmd+lear.+licA )ist*.Items.+lear() End S!" Pri,ate S!" cmdMa!eFile Clic!(=#:al e,entSender As S#stem.-"?ect, =#:al e,entArgs As S#stem.E,entArgs) @andles cmd7aAe0ile.+licA $ Declare ,aria"les. Dim m#Record As em Record Dim Record%!m"er As S5ort Dim rec)en As Integer $ - en file for random access. rec)en 4 )en(m#Record) 0ile- en(*, "c./TEST0I)E.t1t", - en7ode.Random, - enAccess.Write, - enS5are.)ocAWrite, rec)en) $P!t some d!mm# data into t5e file 0or Record%!m"er 4 * To 6 $ )oo 6 times. $ Define ID. m#Record.ID 4 Record%!m"er $ +reate a string. m#Record.%ame 4 "Data-Em lo#ee-" 8 Record%!m"er $ Write record to file. 0ileP!t(*, m#Record, Record%!m"er) $ S5ow t5e record into t5e list"o1 )ist*.Items.Add(m#Record.ID 8 2 " " 8 m#Record.%ame) %e1t Record%!m"er 0ile+lose(*) $ +lose file. $ena"le t5e ot5er command "!ttons cmdRead0ile.Ena"led 4 Tr!e 7e.cmdRandomRead.Ena"led 4 Tr!e End S!" Pri,ate S!" cmdRa$domRead Clic!(=#:al e,entSender As S#stem.-"?ect, =#:al e,entArgs As S#stem.E,entArgs) @andles cmdRandomRead.+licA Dim m#Record As em Record Dim Position As S5ort Dim file%!m As Integer $ - en file (if needed) for random access. -n Error Res!me %e1t file%!m 4 0ree0ile() 0ile- en(file%!m, "c./TEST0I)E.t1t", - en7ode.Random, , , )en(m#Record)) -n Error 9oTo Pro"lems Position 4 :al(t1tPosition.Te1t) If Position ; )-0(file%!m) < )en(m#Record) T5en 9oTo Pro"lems End If 0ile9et(file%!m, m#Record, Position) t1t%ame.Te1t 4 m#Record.%ame 9oTo Adios Pro"lems. 7sg=o1("=ad Inde1 - tr# Again") Adios. 0ile+lose(file%!m) End S!"

Pri,ate S!" cmdReadFile Clic!(=#:al e,entSender As S#stem.-"?ect, =#:al e,entArgs As S#stem.E,entArgs) @andles cmdRead0ile.+licA $ Declare ,aria"les. Dim m#Record As em Record Dim "#tes0ile, Position, As S5ort Dim totalRecords, rec)en As S5ort Dim file%!m As Integer $ - en sam le file for random access. file%!m 4 0ree0ile() rec)en 4 )en(m#Record) 0ile- en(file%!m, "c./TEST0I)E.t1t", - en7ode.Random, , , rec)en) $find o!t 5ow man# records are t5ere in file 3* "#tes0ile 4 )-0(file%!m) totalRecords 4 "#tes0ile < rec)en $ read data from last rec. to first rec. 0or Position 4 totalRecords To * Ste -* 0ile9et(file%!m, m#Record, Position) $ S5ow t5e record into t5e list"o1 )ist*.Items.Add(m#Record.ID 8 " " 8 m#Record.%ame) %e1t 0ile+lose(file%!m) $ +lose file. End S!" Pri,ate S!" frmFile Load(=#:al e,entSender As S#stem.-"?ect, =#:al e,entArgs As S#stem.E,entArgs) @andles 7#=ase.)oad -n Error 9oTo Adios >ill("c./TEST0I)E.t1t") Adios. End S!"

VB6 S1$ta*% /E' 2 P0' S'3'EME&' Reads<Writes data from<to an o en disA file into<from a ,aria"le. S1$ta* 9et B3Cfilen!m"er, Brecn!m"erC, ,arname P!t B3Cfilen!m"er, Brecn!m"erC, ,arname T5e 9et<P!t statement s#nta1 5as t5ese arts. Part filen!m"er recn!m"er ,arname De(criptio$ ReD!ired. An# ,alid file n!m"er. - tional. :ariant ()ong). Record n!m"er (Random mode files) or "#te n!m"er (=inar# mode files) at w5ic5 reading "egins. ReD!ired. :alid ,aria"le name into w5ic5 data is read.

Remar!( Data read wit5 9et is !s!all# written to a file wit5 P!t. T5e first record or "#te in a file is at osition *, t5e second record or "#te is at osition ', and so on. If #o! omit recn!m"er, t5e ne1t record or "#te following t5e last 9et or P!t statement (or ointed to "# t5e last SeeA f!nction) is read. Eo! m!st incl!de delimiting commas, for e1am le. 9et 3F,,0ile=!ffer P!t 3F,,0ile=!ffer 4PE& FILE S'3'EME&' -PE% ena"les in !t<o!t !t (I<-) to a file. S1$ta* - en at5name 0or mode BAccess accessC BlocAC As B3Cfilen!m"er B)en4reclengt5C T5e - en statement s#nta1 5as t5ese arts. Part De(criptio$ at5name ReD!ired. String e1 ression t5at s ecifies a file nameGma# incl!de director# or folder, and dri,e. mode ReD!ired. >e#word s ecif#ing t5e file mode. 3ppe$d5 Bi$ar15 I$put5 4utput, or Ra$dom. If !ns ecified, t5e file is o ened for Random access. access - tional. >e#word s ecif#ing t5e o erations ermitted on t5e o en file. Read, Write, or Read Write. locA - tional. >e#word s ecif#ing t5e o erations ermitted on t5e o en file "# ot5er rocesses. S6ared, Loc! Read, Loc! 7rite, and Loc! Read 7rite. filen!m"er ReD!ired. A ,alid file n!m"er in t5e range * to H**, incl!si,e. Ise t5e FreeFile f!nction to o"tain t5e ne1t a,aila"le file n!m"er. reclengt5 - tional. %!m"er less t5an or eD!al to J',6K6 ("#tes). 0or files o ened for random access, t5is ,al!e is t5e record lengt5. 0or seD!ential files, t5is ,al!e is t5e n!m"er of c5aracters "!ffered.

V. Matos - Input/Output VB.NET Page 3

Visual Basic .NET I/O


File Access Types, Functions, and Statements

T5ese e1cer ts are taAen from t5e L7icrosoft .%ET @el 0acilit#.

=# itself, a file is not5ing more t5an a series of related "#tes of data on a disA. W5en #o!r a lication accesses a file, it m!st ass!me w5et5er t5e "#tes re resent c5aracters, data records, integers, strings, and so on. Eo! tell #o!r a lication w5at to ass!me "# s ecif#ing t5e file$s access t# e. T5e file access t# e #o! !se de ends on w5at Aind of data t5e file contains. :is!al =asic ro,ides t5ree t# es of file access.

SeD!ential, for reading and writing te1t files in contin!o!s "locAs. Random, for reading and writing te1t or "inar# files str!ct!red as fi1ed-lengt5 records. =inar#, for reading and writing ar"itraril# str!ct!red files.

Sequential access is designed for !se wit5 lain te1t files. Eac5 c5aracter in t5e file is ass!med to re resent eit5er a te1t c5aracter or a te1t-formatting seD!ence, s!c5 as a newline c5aracter. Data is stored as A%SI c5aracters. A file o ened for random access is ass!med to "e com osed of a set of identical-lengt5 records, or sets of fields containing information. Eo! can em lo# !ser-defined t# es to create records made ! of n!mero!s fields, eac5 of w5ic5 can 5a,e different data t# es. Data is stored as "inar# information. Binary access allows #o! to !se files to store data in an# form t5at s!its #o!r needs, w5et5er it is n!meric, string, or "ot5. =inar# access is similar to random access e1ce t t5at no ass!m tions are made a"o!t data t# e or record lengt5. @owe,er, #o! m!st Anow recisel# 5ow t5e data was written to t5e file to retrie,e it correctl#. 0or e1am le, if #o! store a series of names and 5one n!m"ers, #o! need to remem"er t5at t5e first field (t5e name) is te1t and t5e second field (t5e 5one n!m"er) is n!meric. File 3cce(( Fu$ctio$( a$d Stateme$t( T5e following f!nctions are !sed wit5 seD!ential, random, and "inar# file access. Dir 0!nction 0ileDateTime 0!nction 9etAttr 0!nction SeeA 0!nction E-0 0!nction 0ile)en 0!nction )oc 0!nction SetAttr 0!nction 0ile+o # 0!nction 0ree0ile 0!nction )-0 0!nction

WorAing wit5 large amo!nts of data often reD!ires t5at data "e written to or read from a file. =efore eit5er o eration can taAe lace, 5owe,er, t5e file m!st first "e o ened. T5e 0ile- en 0!nction lets #o! create and access files wit5 one of t5ree t# es of file access.

SeD!ential access (I$put, 4utput, and 3ppe$d modes) is !sed for writing te1t files, s!c5 as error logs and re orts. Random access (Ra$dom mode) is !sed to read and write data to a file wit5o!t closing it. Random-access files Aee data in records, w5ic5 maAes it eas# to locate information D!icAl#. =inar# access (Bi$ar1 mode) is !sed to read or write to an# "#te osition in a file, s!c5 as storing or dis la#ing a "itma image. Do not !se t5e File4pe$ f!nction to mani !late files t5at are !s!all# edited "# a s ecific a lication, s!c5 as 7icrosoft -ffice doc!ments or registr# files. Doing so will ca!se file corr! tion and loss of file integrit#.

V. Matos - Input/Output VB.NET Page 4

T5e following ta"le s5ows t5e statements t# icall# !sed w5en writing data to and reading it from files. 3cce(( t1pe SeD!ential Random =inar# 7riti$+ data Print, Print)ine 0!nctions 0ileP!t 0!nction 0ileP!t 0!nction Readi$+ data In !tString 0!nction 0ile9et 0!nction 0ile9et 0!nction

FileOpen Function
- ens a file for in !t or o!t !t. Public Sub File4pe$" B1Val FileNumber 3( I$te+er5 B1Val FileName 3( Stri$+5 B1Val Mode 3( 4pe$Mode5 4ptio$al B1Val Access 3( 4pe$3cce(( 8 4pe$3cce((%Default5 4ptio$al B1Val Share 3( 4pe$S6are 8 4pe$S6are%Default5 4ptio$al B1Val RecordLength 3( I$te+er 8 -9 # Parameter( FileNumber ReD!ired. An# ,alid file n!m"er. Ise t5e FreeFile f!nction to o"tain t5e ne1t a,aila"le file n!m"er. FileName ReD!ired. Stri$+ e1 ression t5at s ecifies a file name M ma# incl!de director# or folder, and dri,e. Mode ReD!ired. E$um s ecif#ing t5e file mode. 3ppe$d, Bi$ar1, I$put, 4utput, or Ra$dom. Access - tional. >e#word s ecif#ing t5e o erations ermitted on t5e o en file. Read, 7rite, or Read7rite. Defa!lts to Read7rite. Share - tional. E$um s ecif#ing t5e o erations restricted on t5e o en file "# ot5er rocesses. S6ared, Loc! Read, Loc! 7rite, and Loc! Read 7rite. Defa!lts to S6ared. RecordLength - tional. %!m"er less t5an or eD!al to J',6K6 ("#tes). 0or files o ened for random access, t5is ,al!e is t5e record lengt5. 0or seD!ential files, t5is ,al!e is t5e n!m"er of c5aracters "!ffered. E*ceptio$(2Error( E*ceptio$ t1pe Arg!mentE1ce tion Arg!mentE1ce tion Arg!mentE1ce tion Arg!mentE1ce tion Arg!mentE1ce tion I-E1ce tion I-E1ce tion Error $um% H H H H H H' HH Co$ditio$ In,alid 3cce((5 S6are5 or Mode% 7rite4$l1 file is o ened for I$put% Read4$l1 file is o ened for 4utput% Read4$l1 file is o ened for 3ppe$d% Record lengt5 is negati,e (and not eD!al to -*). FileNumber is in,alid (N-* or ;'HH), or FileNumber is alread# in !se. FileName is alread# o en, or FileName is in,alid.

V. Matos - Input/Output VB.NET Page 5

Remar!( Eo! m!st o en a file "efore an# I<- o eration can "e erformed on it. File4pe$ allocates a "!ffer for I<- to t5e file and determines t5e mode of access to !se wit5 t5e "!ffer. If t5e file s ecified "# FileName doesn$t e1ist, it is created w5en a file is o ened for 3ppe$d, Bi$ar1, 4utput, or Ra$dom modes. T5e c5annel to o en can "e fo!nd !sing t5e FreeFile"# f!nction. E*ample T5is e1am le ill!strates ,ario!s !ses of t5e File4pe$ f!nction to ena"le in !t and o!t !t to a file. T5e following code o ens t5e file

TESTFILE in I$put mode.

File4pe$"*, "TEST0I)E", - en7ode.In !t# $ +lose "efore reo ening in anot5er mode. 0ile+lose(*) T5is e1am le o ens t5e file in Bi$ar1 mode for writing o erations onl#. File4pe$"*, "TEST0I)E", - en7ode.=inar#,- enAccess.Write # $ +lose "efore reo ening in anot5er mode. 0ile+lose(*) T5e following e1am le o ens t5e file in Ra$dom mode. T5e file contains records of t5e str!ct!re Person. Str!ct!re Person N:=0i1edString(J(); Dim %ame As String Dim ID As Integer End Str!ct!re $ +o!nt J( for t5e string, l!s F for t5e integer. File4pe$"*, "TEST0I)E", - en7ode.Random, , , JF) $ +lose "efore reo ening in anot5er mode. 0ile+lose(*) T5is code e1am le o ens t5e file in 4utput modeO an# rocess can read or write to file. File4pe$"*, "TEST0I)E", - en7ode.-!t !t, - enS5are.S5ared# $ +lose "efore reo ening in anot5er mode. 0ile+lose(*) T5is code e1am le o ens t5e file in Bi$ar1 mode for readingO ot5er rocesses can$t read file. File4pe$"*, "TEST0I)E", - en7ode.=inar#, - enAccess.Read, - enS5are.)ocARead )

V. Matos - Input/Output VB.NET Page 6

Opening Files for Se uential Access


Ising S1(tem%I4 is t5e recommended wa# to create te1t files, "!t s5o!ld #o! need to !se older te1t file-creation met5ods, t5is section s5ows #o! 5ow. SeD!ential access worAs "est w5en #o! want to rocess files consisting onl# of te1t, s!c5 as files created wit5 a t# ical te1t editor, not files in w5ic5 data is di,ided into a series of records. SeD!ential access is not well s!ited for storing long series of n!m"ers, "eca!se eac5 n!m"er is stored as a c5aracter string. T5!s, a fo!r-digit n!m"er wo!ld reD!ire fo!r "#tes of storage instead of t5e two "#tes reD!ired to store t5e same n!m"er as an integer. W5en #o! o en a file for seD!ential access, #o! m!st s ecif# w5et5er to in !t c5aracters from t5e file ( I$put mode), o!t !t c5aracters to t5e file (4utput mode), or a end c5aracters to t5e file (3ppe$d mode). 'o ope$ a file for (e:ue$tial acce((

Ise t5e 0ile- en 0!nction wit5 t5e following s#nta1. File4pe$(FileNumber, FileName, 4pe$Mode.I$put)

W5en #o! o en a seD!ential file for I$put, t5e file m!st alread# e1istO ot5erwise an error occ!rs. W5en #o! tr# to o en a none1istent file for 4utput or 3ppe$d, 5owe,er, t5e File4pe$ statement creates t5e file first and t5en o ens it. After o ening a file for an I$put, 4utput, or 3ppe$d o eration, #o! m!st close it wit5 t5e 0ile+lose 0!nction statement "efore reo ening it for anot5er t# e of o eration.

Editing Files Opened for Se uential Access


To edit a file, #o! m!st first read its contents to rogram ,aria"les, t5en c5ange t5e ,aria"les, and finall#, write t5e ,aria"les "acA to t5e file. T5e following sections disc!ss 5ow to edit records o ened for seD!ential access. 'o read (tri$+( from file( *. '. Retrie,e t5e contents of a te1t file "# o ening it for I$put. Ise t5e )ineIn !t, In !tString, or In !t f!nctions to co # t5e file into rogram ,aria"les.

:is!al =asic ro,ides statements and f!nctions t5at read and write seD!ential files one c5aracter at a time or one line at a time. 0or e1am le, t5e following code fragment reads a file line "# line. Dim )ines0rom0ile, %e1t)ine As String, 0ile%!m As Integer Do Intil E-0(0ile%!m) )ineIn !t(0ile%!m, %e1t)ine) )ines0rom0ile 4 )ines0rom0ile 8 %e1t)ine 8 +5r(*J) 8 +5r(*() )oo Alt5o!g5 t5e Li$eI$put f!nction recogniPes t5e end of a line w5en it comes to t5e carriage ret!rn<line feed seD!ence, it does not incl!de t5at seD!ence w5en it reads t5e line into t5e ,aria"le. If #o! want to retain t5e carriage ret!rn<line feed, #o!r code m!st add it. Eo! can !se t5e I$putStri$+ f!nction to co # a s ecified n!m"er of c5aracters from a file to a ,aria"le, ro,ided t5e ,aria"le is large eno!g5. 0or e1am le, t5e following code !ses I$putStri$+ to co # +5ar+o!nt n!m"er of c5aracters to a ,aria"le.

V. Matos - Input/Output VB.NET Page 7

)ines0rom0ile 4 In !tString(0ile%!m, +5ar+o!nt) Eo! can also !se t5e I$put f!nction, w5ic5 reads a list of n!m"ers and<or string e1 ressions from a file. 0or e1am le, to read in a line from a mailing list file, #o! mig5t !se t5e following statements. In In In In In In !t(0ile%!m, !t(0ile%!m, !t(0ile%!m, !t(0ile%!m, !t(0ile%!m, !t(0ile%!m, fname) lname) street) cit#) state) Pi )

!riting Strings to Se uential"Access Files


Eo! can add data in t5e form of strings to e1isting files ,ia t5e Print 0!nction or in t5e form of n!m"ers and string e1 ressions t5ro!g5 t5e Write 0!nction. 'o ;rite (tri$+( to file( *. '. Ise t5e 0ile- en 0!nction to o en a te1t file for 4utput or 3ppe$d. Ise t5e Pri$t f!nction to write t5e strings to t5e file as in t5e following e1am le, w5ic5 a te1t editor mig5t !se to co # t5e contents of a te1t "o1 into a file. Print(0ile%!m, T5e=o1.Te1t) :is!al =asic also s! orts t5e 7rite f!nction, w5ic5 writes a list of n!m"ers and<or string e1 ressions to a file. It a!tomaticall# se arates eac5 e1 ression wit5 a comma and !ts D!otation marAs aro!nd Stri$+ e1 ressions, as ill!strated in t5e following code. Dim An#String As String, An#%!m"er, 0ile%!m As Integer An#String 4 "An#+5aracters" An#%!m"er 4 'JFFH Write(0ile%!m, An#String, An#%!m"er) T5e re,io!s code segment writes two e1 ressions to t5e file s ecified "# 0ile%!m. T5e first contains a string, and t5e second contains t5e n!m"er 'JFFH. T5erefore, :is!al =asic writes t5e following c5aracters (incl!ding all !nct!ation) to t5e file. "An#+5aracters",'JFFH &ote If #o! are maintaining a data"ase !sing 7rite and I$put wit5 seD!ential access, consider !sing FilePut4b<ect and File/et4b<ect wit5 random or "inar# access insteadO "ot5 t# es of access are "etter s!ited to record-oriented data. -ne of t5e rimar# differences "etween Pri$t and 7rite is t5at Pri$t formats t5e string, w5ile 7rite does not. 0or t5is reason, Pri$t does not incl!de D!otation marAs aro!nd t5e string, w5ic5 is formatted as t5e !ser wo!ld liAe to see it, w5ile 7rite does. Ise a f!nction t5at writes eac5 string to a new line, s!c5 as Pri$tLi$e or 7riteLi$e if #o! need strings se arated "# line.

V. Matos - Input/Output VB.NET Page 8

#andom File Access


T5e "#tes in random-access files form records of !niform lengt5, eac5 containing one or more fields. A record wit5 one field corres onds to an# standard t# e, s!c5 as an integer or fi1ed-lengt5 string. A record wit5 more t5an one field corres onds to a !ser-defined t# e. In t5is section #o! declare, o en, edit, and write to a random-access file, !sing a 5# ot5etical Em lo#ee Records data"ase as an e1am le. Eac5 file-access as ect of t5e e1am le is disc!ssed se aratel# in t5e to ics listed immediatel# "elow.

$eclaring Varia%les for #andom File Access


=efore #o!r a lication o ens a file for random access, it s5o!ld declare all ,aria"les reD!ired to 5andle data from t5e file. T5is incl!des !ser-defined t# es, w5ic5 corres ond to records in t5e file, as well as standard t# es for ot5er ,aria"les t5at 5old data related to rocessing a random-access file. =efore o ening a file for random access, define a str!ct!re t5at corres onds to t5e records t5e file contains or will contain. 0or e1am le, a 5# ot5etical Em lo#ee Records data"ase mig5t contain a !ser-defined data t# e called Person as follows. Str!ct!re Person P!"lic ID As Integer P!"lic 7ont5l#Salar# As Decimal P!"lic )astRe,iewDate As )ong N:=0i1edString(*H); P!"lic 0irst%ame As String N:=0i1edString(*H); P!"lic )ast%ame As String N:=0i1edString(*H); P!"lic Title As String N:=0i1edString(*H(); P!"lic Re,iew+omments As String End Str!ct!re

$eclaring Field Varia%les in a Structure $efinition


=eca!se all records in a random-access file m!st 5a,e t5e same lengt5, it is often !sef!l for string elements in a !ser-defined t# e to 5a,e a fi1ed lengt5 also. In t5e Person t# e declaration a"o,e, for e1am le, 0irst%ame and )ast%ame 5a,e a fi1ed lengt5 of *H c5aracters. To declare a fi1ed-lengt5 string, set its lengt5 wit5 t5e VBFi*edStri$+ attri"!te. If t5e act!al string contains fewer c5aracters t5an t5e fi1ed lengt5 of t5e string element to w5ic5 it is written, :is!al =asic fills t5e trailing s aces in t5e record wit5 "lanAs (c5aracter code J'). If t5e string is longer t5an t5e field siPe, :is!al =asic tr!ncates it. (If #o! !se ,aria"le-lengt5 strings, t5e total siPe of an# record stored wit5 t5e 0ileP!t 0!nction or retrie,ed wit5 t5e 0ile9et 0!nction m!st not e1ceed t5e record lengt5 s ecified in t5e 0ile- en 0!nction). After defining a str!ct!re t5at corres onds to a t# ical record, declare an# ot5er ,aria"les t5at #o!r a lication needs to rocess a file o ened for random access. T5e Em lo#ee Records data"ase, for instance, declares Em lo#ee, Position, and )astRecord ,aria"les, as follows. $ Declare a record ,aria"le. P!"lic Em lo#ee As Person $ TracA t5e c!rrent record. P!"lic Position As )ong $ 9et t5e n!m"er of t5e last record in t5e file. P!"lic )astRecord As )ong

V. Matos - Input/Output VB.NET Page 9

Opening Files for #andom Access


After #o! create t5e file, #o! need to o en it to maAe an# modifications. 'o ope$ a file for ra$dom acce((

Ise t5e 0ile- en 0!nction wit5 t5e following s#nta1. File4pe$(FileNumber, FileName, 4pe$Mode.Ra$dom, , , RecordLength)

FileNumber and FileName s ecif# t5e file n!m"er and name of t5e file to "e o ened, res ecti,el#. RecordLength s ecifies t5e siPe of eac5 record in "#tes. If RecordLength is less t5an t5e act!al lengt5 of t5e record written to t5e file, an error is generated. If RecordLength is greater t5an t5e act!al lengt5 of t5e record, t5e record is written, alt5o!g5 some disA s ace ma# "e wasted. %ote t5at e,er# Stri$+ ,aria"le in :is!al =asic stores an AS+II string, and t5at #o! m!st s ecif# t5e "#te lengt5 of t5at AS+II string. In Declaring :aria"les for Random 0ile Access, a 5# ot5etical Em lo#ee Records data"ase first defined a Person data t# e and t5en declared a record ,aria"le of t5at t# e as well as two ot5er ,aria"les for rocessing records. T5e following code contin!es t5e Em lo#ee Records e1am le, demonstrating 5ow to o en a random-access file to acce t Em lo#ee data of !ser-defined t# e Person. S!" 7ain() Dim 0ile%!m As Integer, Rec)engt5 As )ong, Em lo#ee As Person $ +alc!late t5e record lengt5. Rec)engt5 4 )en(Em lo#ee) $ 9et t5e ne1t a,aila"le file n!m"er. 0ile%!m 4 0ree0ile $ - en t5e new file wit5 t5e 0ile- en statement. 0ile- en(0ile%!m, "7E0I)E.DAT", - en7ode.Random, , , Rec)engt5) End S!"

Editing Files Opened for #andom Access


To edit a random-access file, first read records from t5e file into rogram ,aria"les, and t5en c5ange t5e ,al!es in t5e ,aria"les. In Declaring :aria"les for Random 0ile Access and - ening 0iles for Random Access, a 5# ot5etical Em lo#ee Records data"ase defined ,aria"les for reading and writing em lo#ee records. T5e following ste descri"es 5ow to read and co # em lo#ee records !sing t5ose ,aria"les. 'o read a$d cop1 record( i$to variable(

Ise t5e 0ile9et-"?ect 7et5od, w5ic5 co ies a record from t5e Em lo#ee Records file into t5e Em lo#ee ,aria"le. 0ile9et(0ile%!m, Em lo#ee, Position) In t5is line of code, FileNum contains t5e n!m"er t5at t5e 0ile- en 0!nction !sed to o en t5e file, Position contains t5e n!m"er of t5e record to co #, and Employee, declared as t5e !ser-defined t# e Person, recei,es t5e contents of t5e record.

!riting Varia%les to #ecords

V. Matos - Input/Output VB.NET Page 10

After editing records read into rogram ,aria"les from a random-access file, !se t5e 0ileP!t 0!nction to re lace or add records. In Declaring :aria"les for Random 0ile Access and - ening 0iles for Random Access, a 5# ot5etical Em lo#ee Records data"ase defined ,aria"les for reading and writing em lo#ee records. T5e following ste s descri"e 5ow to re lace and add em lo#ee records !sing t5ose ,aria"les. 'o replace a record

Ise FilePut to s ecif# t5e osition of t5e record #o! want to re lace. T5e Random 0ile Access e1am le ill!strates t5is in t5e following code. 0ileP!t(0ile%!m, Em lo#ee, Position) T5is code re laces t5e record n!m"er s ecified "#

Position wit5 t5e data in t5e Employee ,aria"le.

'o add a record

Ising FilePut, set t5e ,al!e of t5e Position ,aria"le eD!al to one more t5an t5e n!m"er of records in t5e file. To add a record to a file t5at contains fi,e records, for e1am le, set Position eD!al to K. In t5e case of t5e Em lo#ee Records e1am le, s!"stit!ting t5e following statements for t5e FilePut statement a"o,e adds a record to t5e end of t5e file instead of o,erwriting t5e one s ecified "# Position. )astRecord 4 )astRecord Q * 0ileP!t(0ile%!m, Em lo#ee, )astRecord)

Deleti$+ Record( Eo! can delete a record$s contents "# clearing its fields, "!t t5e record will still e1ist in t5e file. In most cases, #o! don$t want em t# records in #o!r file "eca!se t5e# waste s ace. To a,oid t5is, #o! can co # t5e remaining records to a new file, and t5en delete t5e old oneO or, #o! can maAe a note of t5e location of t5e em t# record and !se t5at slot on t5e ne1t insert o eration. 'o delete a record

+lear t5e record$s fields.

'o remove a deleted record *. '. J. F. +reate a new file. +o # all t5e ,alid records from t5e original file into t5e new file. +lose t5e original file and !se t5e =ill f!nction to delete it. Ise t5e Re$ame f!nction to rename t5e new file wit5 t5e name of t5e original file.

V. Matos - Input/Output VB.NET Page 11

FileLe$ Fu$ctio$
Ret!rns a Lo$+ ,al!e s ecif#ing t5e lengt5 of a file in "#tes. Public Fu$ctio$ FileLe$"B1Val PathName 3( Stri$+# 3( Lo$+ Parameter PathName ReD!ired. Stri$+ e1 ression t5at s ecifies a file. PathName ma# incl!de t5e director# or folder, and t5e dri,e. E*ceptio$(2Error( E*ceptio$ t1pe 0ile%ot0o!ndE1ce tion Remar!( If t5e s ecified file is o en w5en t5e FileLe$ f!nction is called, t5e ,al!e ret!rned re resents t5e siPe of t5e file at t5e time it was o ened. &ote To o"tain t5e c!rrent lengt5 of an o en file, !se t5e L4F f!nction. Error $umber HJ Co$ditio$ 0ile does not e1ist.

E*ample T5is e1am le !ses t5e FileLe$ f!nction to ret!rn t5e lengt5 of a file in "#tes. 0or !r oses of t5is e1am le, ass!me t5at TEST0I)E is a file containing some data. Dim 7#SiPe As )ong 7#SiPe 4 FileLe$""TEST0I)E"#

$ Ret!rns file lengt5 ("#tes).

V. Matos - Input/Output VB.NET Page 1

L4F Fu$ctio$
Ret!rns a Lo$+ re resenting t5e siPe, in "#tes, of a file o ened !sing t5e File4pe$ f!nction. Public Fu$ctio$ L4F"B1Val FileNumber 3( I$te+er# 3( Lo$+ Parameter FileNumber ReD!ired. An I$te+er containing a ,alid file n!m"er. E*ceptio$(2Error( E*ceptio$ t1pe I-E1ce tion I-E1ce tion Remar!( Ise t5e FileLe$ f!nction to o"tain t5e lengt5 of a file t5at is not o en. E*ample T5is e1am le !ses t5e L4F f!nction to determine t5e siPe of an o en file. T5is e1am le ass!mes t5at TEST0I)E is a te1t file containing sam le data. Dim lengt5 As Integer 0ile- en(*, "+./TEST0I)E.TRT", - en7ode.In !t) $ - en file. lengt5 4 L4F"*# $ 9et lengt5 of file. De"!g.Write)ine(lengt5) 0ile+lose(*) $ +lose file. Error $umber H' HF Co$ditio$ FileNumber does not e1ist. 0ile mode is in,alid.

V. Matos - Input/Output VB.NET Page 13

Binary File Access


Ise "inar# access w5en it is im ortant to Aee file siPe small. =eca!se "inar# access doesn$t reD!ire fi1ed-lengt5 fields, t5e t# e declaration can omit t5e string lengt5 arameters. T5is ena"les #o! to conser,e disA s ace "# "!ilding ,aria"le-lengt5 records. 4pe$i$+ a File for Bi$ar1 3cce(( Ise "inar# access w5en it is im ortant to Aee file siPe small. =eca!se "inar# access doesn$t reD!ire fi1ed-lengt5 fields, #o! can conser,e disA s ace "# "!ilding ,aria"le-lengt5 records. 'o ope$ a file for bi$ar1 acce((

Ise t5e 0ile- en 0!nction wit5 t5e following s#nta1. File4pe$(FileNumber, FileName, 4pe$Mode.Bi$ar1)

As #o! can see, o ening files for "inar# access differs from - ening 0iles for Random Access in t5at t5e RecordLength e1 ression is not s ecified. (If #o! do incl!de a record lengt5 in a "inar#-access File4pe$ f!nction, it is ignored.) To a reciate "inar# access, consider t5e Em lo#ee Records e1am le disc!ssed in t5e Random 0ile Access section. T5is random-access file !ses fi1ed-lengt5 records and fields to store information a"o!t em lo#ees. As a res!lt, e,er# record in t5e Em lo#ee Records data"ase taAes t5e same n!m"er of "#tes, regardless of t5e act!al contents of t5e fields. Eo! can minimiPe t5e siPe of Em lo#ee Records "# !sing "inar# access. =eca!se #o! are not reD!ired to !se fi1edlengt5 fields, #o! can omit t5e string-lengt5 arameters from t5e t# e declaration, as in t5e following code. Str!ct!re Person ID As Integer 7ont5l#Salar# As Decimal )astRe,iewDate As )ong 0irst%ame As String )ast%ame As String Title As String Re,iew+omments As String End Str!ct!re P!"lic Em lo#ee As Person $ Defines a record.

Eac5 em lo#ee record in t5e Em lo#ee Records file now occ! ies onl# t5e e1act n!m"er of "#tes reD!ired, "eca!se t5e fields are of ,aria"le lengt5s. T5e draw"acA to "inar# in !t<o!t !t wit5 ,aria"le-lengt5 fields is t5at #o! cannot randoml# access recordsO #o! m!st instead access t5em seD!entiall# to learn t5e lengt5 of eac5 one. Eo! can still go directl# to a s ecified "#te osition in a file, "!t if t5e fields are of ,aria"le lengt5, t5ere is no direct wa# to Anow w5ic5 record is at w5ic5 "#te osition.

V. Matos - Input/Output VB.NET Page 14

Accessing Files &it' FileSystemO%(ect


T5e 0ile S#stem -"?ect (0S-) model ro,ides an o"?ect-"ased tool for worAing wit5 folders and files. It allows #o! to !se t5e familiar object.method s#nta1 wit5 a ric5 set of ro erties, met5ods, and e,ents to rocess folders and files. Eo! can also em lo# t5e traditional :is!al =asic statements and commands. T5e 0S- model gi,es #o!r a lication t5e a"ilit# to create, alter, mo,e, and delete folders, or to determine if and w5ere artic!lar folders e1ist. It also ena"les #o! to get information a"o!t folders, s!c5 as t5eir names and t5e date t5e# were created or last modified. T5e 0S- model maAes rocessing files m!c5 easier as well. W5en rocessing files, #o!r rimar# goal is to store data in an efficient, eas#-to-access format. Eo! need to "e a"le to create files, insert and c5ange t5e data, and o!t !t (read) t5e data. Alt5o!g5 #o! can store data in a data"ase, doing so adds a significant amo!nt of o,er5ead to #o!r a lication. Eo! ma# not want to 5a,e s!c5 o,er5ead, or #o!r data access reD!irements ma# not call for t5e e1tra f!nctionalit# associated wit5 a f!ll-feat!red data"ase. In t5is case, storing #o!r data in a te1t file or "inar# file is t5e most efficient sol!tion. T5e 0S- model, contained in t5e Scri ting t# e li"rar# (Scrr!n.dll), s! orts t5e creation and mani !lation of te1t files t5ro!g5 t5e 'e*tStream o"?ectO 5owe,er, t5e 0S- model does not s! ort "inar# files. To mani !late "inar# files, !se t5e 0ile- en 0!nction wit5 t5e Bi$ar1 Ae#word. T5e following o"?ects maAe ! t5e 0S- model. 4b<ect FileS1(tem4b<ect De(criptio$ Allows #o! to create, delete, gain information a"o!t, and generall# mani !late dri,es, folders, and files. 7an# of t5e met5ods associated wit5 t5is o"?ect d! licate t5ose in t5e ot5er o"?ects. Allows #o! to gat5er information a"o!t a dri,e attac5ed to t5e s#stem, s!c5 as 5ow m!c5 room is a,aila"le and w5at its s5are name is. %ote t5at a "dri,e" !nder t5e 0Smodel isn$t necessaril# a 5ard disA. it can "e a +D-R-7 dri,e, a RA7 disA, and so fort5. A dri,e also isn$t reD!ired to "e 5#sicall# attac5ed to t5e s#stemO it can also "e logicall# connected t5ro!g5 a local area networA ()A%). Allows #o! to create, delete, or mo,e folders, as well as D!er# t5e s#stem a"o!t t5eir names, at5s, and ot5er information. Allows #o! to create, delete, or mo,e files, as well as D!er# t5e s#stem a"o!t t5eir names, at5s, and ot5er information. Ena"les #o! to read and write te1t files.

Drive

Folder File 'e*tStream

0or information a"o!t t5e ,ario!s ro erties, met5ods, and e,ents in t5e 0S- model, !se t5e -"?ect =rowser in :is!al =asic "# ressing +TR)QA)TQS and looAing at t5e Scri ting t# e li"rar#. If t5e Scri ting t# e li"rar# does not a ear in t5e list, create a reference to it as s5own in t5e ne1t section. Pro+rammi$+ i$ t6e FS4 Model Programming in t5e 0S- model entails t5ree main tasAs.

+reating a FileS1(tem4b<ect o"?ect "# !sing t5e Create4b<ect met5od, or "# dimensioning a ,aria"le as a FileS1(tem4b<ect o"?ect. Ising t5e a ro riate met5od on t5e newl# created o"?ect. Accessing t5e o"?ect$s ro erties.

T5e 0S- model is contained in t5e Scri ting t# e li"rar#, w5ic5 is located in t5e file Scrr!n.dll. If #o! don$t alread# 5a,e a reference to it, #o! can create one. 'o create a refere$ce to t6e Scripti$+ t1pe librar1 "Scrru$%dll#

V. Matos - Input/Output VB.NET Page 15

*. '.

-n t5e Pro<ect men!, clicA 3dd Refere$ce, and t5en clicA t5e C4M ta". +5oose Micro(oft Scripti$+ Ru$time from t5e Compo$e$t &ame list, and t5en clicA Select. Eo! can now !se t5e -"?ect =rowser to ,iew t5e 0S- model$s o"?ects, collections, ro erties, met5ods, e,ents, and constants.

'o create a FileS1(tem4b<ect ob<ect

Dimension a ,aria"le as t# e FileS1(tem4b<ect, as in t5e following code. Dim fso As %ew 0ileS#stem-"?ect - or -

Ise t5e Create4b<ect met5od to create a FileS1(tem4b<ect o"?ect, as in t5e following code. fso 4 +reate-"?ect("Scri ting.0ileS#stem-"?ect")

In t5e second e1am le, Scripting is t5e name of t5e t# e li"rar#, and FileS1(tem4b<ect is t5e name of t5e o"?ect of w5ic5 #o! want to create an instance. &ote T5e first met5od worAs onl# in :is!al =asic, w5ile t5e second met5od worAs eit5er in :is!al =asic or :=Scri t. FS4 Met6od( T5e following ta"le s5ows 0S- met5ods and t5e tasAs t5at t5e# erform. 'a(! Comma$d +reate a new o"?ect CreateFolder or Create'e*tFile Delete a file or folder DeleteFile or File%DeleteO DeleteFolder or Folder%Delete +o # an o"?ect Cop1File or File%Cop1O Cop1Folder or Folder%Cop1 7o,e an o"?ect MoveFile or File%MoveO MoveFolder or Folder%Move Access an e1isting dri,e, folder, or file /etDrive, /etFolder, or /etFile &ote T5e 0S- model does not s! ort t5e creation or deletion of dri,es. As #o! can see, some f!nctionalit# in t5e FileS1(tem4b<ect o"?ect model is red!ndant. 0or e1am le, #o! can co # a file !sing eit5er t5e Cop1File met5od of t5e FileS1(tem4b<ect o"?ect, or #o! can !se t5e Cop1 met5od of t5e File o"?ect. T5e met5ods worA t5e same wa#O "ot5 ,ersions are incl!ded to gi,e #o! ma1im!m rogramming fle1i"ilit#. %ote, 5owe,er, t5at #o! don$t need to !se t5e /et met5ods for a newl# created o"?ect, since t5e Create f!nctions alread# ret!rn a 5andle to it. 0or e1am le, if #o! create a new folder !sing t5e CreateFolder met5od, #o! don$t need to !se t5e /etFolder met5od to access its ro erties, s!c5 as &ame, Pat6, or Si>e. S!st set a ,aria"le to t5e CreateFolder f!nction to gain a 5andle to t5e newl# created folder, and t5en access its ro erties, met5ods, and e,ents. Drive I$formatio$ T5e Drive o"?ect allows #o! to get information a"o!t t5e ,ario!s dri,es attac5ed to a s#stem, eit5er 5#sicall# or o,er a networA. Its ro erties contain t5e following information. Propert1 'otalSi>e 3vailableSpace , FreeSpace DriveLetter De(criptio$ Total siPe of t5e dri,e, in "#tes @ow m!c5 s ace is a,aila"le on t5e dri,e, in "#tes )etter assigned to t5e dri,e

V. Matos - Input/Output VB.NET Page 16

Drive'1pe Serial&umber FileS1(tem I(Read1 S6are&ame, Volume&ame Pat6, RootFolder 3pp%Pat65 C6Dir5 C6Drive5 a$d CurDir

T# e of dri,e (remo,a"le, fi1ed, networA, +D-R-7, or RA7 disA) Dri,e$s serial n!m"er T# e of file s#stem t5e dri,e !ses (0AT, 0ATJ', or %T0S) W5et5er a dri,e is a,aila"le for !se %ame of t5e s5are and<or ,ol!me Pat5 or root folder of t5e dri,e

If #o! !se t5e Pat6 ro ert# (3pp%Pat6), t5e C6Drive and C6Dir statements, or t5e CurDir f!nction, "e aware t5at t5e# ma# ret!rn a Ini,ersal %aming +on,ention (I%+) at5 (t5at is, one "eginning wit5 // Server/ShareT) rat5er t5an a dri,e at5 (s!c5 as e./Folder), de ending on 5ow #o! r!n #o!r rogram or ro?ect. 3pp%Pat6 ret!rns a I%+ at5 w5en.

Eo! r!n a ro?ect after loading it from a networA s5are, e,en if t5e networA s5are is ma ed to a dri,e letter. Eo! r!n a com iled e1ec!ta"le file from a networA s5are ("!t onl# if it is r!n !sing a I%+ at5).

C6Drive cannot 5andle I%+ at5s, and it raises an error w5en 3pp%Pat6 ret!rns one. Eo! can 5andle t5is error "# adding -n Error Res!me %e1t "efore t5e C6Drive statement, or "# testing t5e first two c5aracters of 3pp%Pat6 to see if t5e# are "acAslas5es. T5e command rom t alwa#s 5as a dri,e at5 for t5e c!rrent director#, so CurDir is set to a dri,e at5. C6Dir does not raise an error, "!t it fails to c5ange t5e director# from a dri,e at5 to a I%+ at5. T5e onl# worAaro!nd for t5is sit!ation is to locate a local dri,e t5at is ma ed to t5e s5are s ecified in t5e I%+ at5, or to !se networA commands to create s!c5 a ma ing. If t5e ro?ect is loaded into t5e :is!al =asic de,elo ment en,ironment from a networA s5are M eit5er a I%+ at5 or a ma ed dri,e at5 M 3pp%Pat6 ret!rns a I%+ at5 w5en t5e ro?ect is r!n and C6Drive fails and raises an error. C6Dir does not raise an error, "!t it fails to c5ange t5e director#. T5e onl# worAaro!nd for t5is sit!ation is to man!all# set t5e dri,e and director#. If more t5an one erson can o en t5e ro?ect on t5e networA s5are, #o! can !se an 7S-D-S en,ironment ,aria"le to gi,e eac5 erson a c!stomiPed ma ing for t5e s5are. Folder I$formatio$ T5e following ta"le s5ows t5e met5ods for carr#ing o!t common folder tasAs. Met6od FileS1(tem4b<ect%CreateFolder Folder%Delete or FileS1(tem4b<ect%DeleteFolder Folder%Move or FileS1(tem4b<ect%MoveFolder Folder%Cop1 or FileS1(tem4b<ect%Cop1Folder Folder%&ame FileS1(tem4b<ect%FolderE*i(t( FileS1(tem4b<ect%/etFolder 'a(! +reate a folder. Delete a folder. 7o,e a folder. +o # a folder.

Retrie,e t5e name of a folder. 0ind o!t if a folder e1ists on a dri,e. 9et an instance of an e1isting Folder o"?ect. FileS1(tem4b<ect%/etPare$tFolder&ame 0ind o!t t5e name of a folder$s arent folder. FileS1(tem4b<ect%/etSpecialFolder 0ind o!t t5e at5 of a s#stem folder. Se:ue$tial 'e*t File( a$d FS4

V. Matos - Input/Output VB.NET Page 17

SeD!ential te1t files (sometimes referred to as a te t stream) are !sef!l w5en #o! want to read t5e contents of a file wit5in a FileS1(tem4b<ect o"?ect. Eo! can access seD!ential te1t files wit5 t5ese met5ods. Met6od Create'e*tFile , 4pe$'e*tFile, or 4pe$3('e*tStream 7rite or 7riteLi$e Read, ReadLi$e, or Read3ll File%Move or MoveFile File%Cop1 or Cop1File File%Delete or DeleteFile 'a(! +reate a seD!ential te1t file. Add data to a te1t file. Read data from a te1t file. 7o,e a file. +o # a file. Delete a file.

S1(tem%I4 &ame(pace T5e I- names ace contains t# es t5at allow s#nc5rono!s and as#nc5rono!s reading and writing on data streams and files. %ames ace 5ierarc5# Cla((e( Cla(( =inar#Reader =inar#Writer =!fferedStream Director# Director#Info Director#%ot0o!ndE1ce tion End-fStreamE1ce tion ErrorE,entArgs 0ile De(criptio$ Reads rimiti,e data t# es as "inar# ,al!es in a s ecific encoding. Writes rimiti,e t# es in "inar# to a stream and s! orts writing strings in a s ecific encoding. Reads and writes to anot5er stream. T5is class cannot "e in5erited. E1 oses static met5ods for creating, mo,ing, and en!merating t5ro!g5 directories and s!"directories. E1 oses instance met5ods for creating, mo,ing, and en!merating t5ro!g5 directories and s!"directories. T5e e1ce tion t5at is t5rown w5en art of a file or director# cannot "e fo!nd. T5e e1ce tion t5at is t5rown w5en reading is attem ted ast t5e end of a stream. Pro,ides data for t5e Error e,ent. Pro,ides static met5ods for t5e creation, co #ing, deletion, mo,ing, and o ening of files, and aids in t5e creation of 0ileStream o"?ects. Pro,ides instance met5ods for t5e creation, co #ing, deletion, mo,ing, and o ening of files, and aids in t5e creation of 0ileStream o"?ects. T5e e1ce tion t5at is t5rown w5en a file is fo!nd "!t cannot "e loaded. T5e e1ce tion t5at is t5rown w5en an attem t to access a file t5at does not e1ist on disA fails. E1 oses a Stream aro!nd a file, s! orting "ot5 s#nc5rono!s and as#nc5rono!s read and write o erations. Pro,ides data for t5e director# e,ents. +5anged, +reated, Deleted. Pro,ides t5e "ase class for "ot5 0ileInfo and Director#Info o"?ects. )istens to t5e file s#stem c5ange notifications and raises e,ents w5en a director#, or file in a director#, c5anges.

0ileInfo

0ile)oadE1ce tion 0ile%ot0o!ndE1ce tion 0ileStream

0ileS#stemE,entArgs 0ileS#stemInfo 0ileS#stemWatc5er

V. Matos - Input/Output VB.NET Page 18

Internal=!ffer-,erflowE1ce tion I-Descri tionAttri"!te I-E1ce tion 7emor#Stream Pat5

Pat5Too)ongE1ce tion

RenamedE,entArgs Stream StreamReader StreamWriter StringReader StringWriter Te1tReader Te1tWriter

T5e e1ce tion t5rown w5en t5e internal "!ffer o,erflows. Sets t5e descri tion ,is!al designers can dis la# w5en referencing an e,ent, e1tender, or ro ert#. T5e e1ce tion t5at is t5rown w5en an I<- error occ!rs. +reates a stream w5ose "acAing store is memor#. Performs o erations on String instances t5at contain file or director# at5 information. T5ese o erations are erformed in a cross- latform manner. T5e e1ce tion t5at is t5rown w5en a at5name or filename is longer t5an t5e s#stem-defined ma1im!m lengt5 . Pro,ides data for t5e Renamed e,ent. Pro,ides a generic ,iew of a seD!ence of "#tes. Im lements a Te1tReader t5at reads c5aracters from a "#te stream in a artic!lar encoding. Im lements a Te1tWriter for writing c5aracters to a stream in a artic!lar encoding. Im lements a 'e*tReader t5at reads from a string. Writes information to a string. T5e information is stored in an !nderl#ing String=!ilder. Re resents a reader t5at can read a seD!ential series of c5aracters. Re resents a writer t5at can write a seD!ential series of c5aracters. T5is class is a"stract.

Structure( Structure Wait0or+5angedRes!lt De(criptio$ +ontains information on t5e c5ange t5at occ!rred.

Dele+ate( Dele+ate ErrorE,ent@andler 0ileS#stemE,ent@andler RenamedE,ent@andler De(criptio$ Re resents t5e met5od t5at will 5andle t5e Error e,ent of a 0ileS#stemWatc5er. Re resents t5e met5od t5at will 5andle t5e +5anged, +reated, or Deleted e,ent of a 0ileS#stemWatc5er class. Re resents t5e met5od t5at will 5andle t5e Renamed e,ent of a 0ileS#stemWatc5er class.

E$umeratio$( E$umeratio$ 0ileAccess 0ileAttri"!tes 0ile7ode 0ileS5are %otif#0ilters SeeA-rigin Watc5er+5angeT# es De(criptio$ Defines constants for read, write, or read<write access to a file. Pro,ides attri"!tes for files and directories. S ecifies 5ow t5e o erating s#stem s5o!ld o en a file. +ontains constants for controlling t5e Aind of access ot5er 0ileStreams can 5a,e to t5e same file. S ecifies c5anges to watc5 for in a file or folder. Pro,ides t5e fields t5at re resent reference oints in streams for seeAing. +5anges t5at ma# occ!r to a file or director#.

V. Matos - Input/Output VB.NET Page 19

FileStream Member( Public Co$(tructor( -,erloaded. InitialiPes a new instance of t5e FileStream class.

0ileStream +onstr!ctor Public Propertie(

+anRead +anSeeA +anWrite @andle IsAs#nc )engt5 %ame Position Public Met6od(

-,erridden. 9ets a ,al!e indicating w5et5er t5e c!rrent stream s! orts reading. -,erridden. 9ets a ,al!e indicating w5et5er t5e c!rrent stream s! orts seeAing. -,erridden. 9ets a ,al!e indicating w5et5er t5e c!rrent stream s! orts writing. 9ets t5e o erating s#stem file 5andle for t5e file t5at t5e c!rrent FileStream o"?ect enca s!lates. 9ets a ,al!e indicating w5et5er t5e FileStream was o ened as#nc5rono!sl# or s#nc5rono!sl#. -,erridden. 9ets t5e lengt5 in "#tes of t5e stream. 9ets t5e name of t5e FileStream t5at was assed to t5e constr!ctor. -,erridden. 9ets or sets t5e c!rrent osition of t5is stream.

-,erridden. =egins an as#nc5rono!s read. =eginRead -,erridden. =egins an as#nc5rono!s write. =eginWrite +lose +reate-"?Ref (in5erited from Mar(6alB1Ref4b<ect) EndRead EndWrite ED!als (in5erited from 4b<ect) 0l!s5 9et@as5+ode (in5erited from 4b<ect) -,erridden. +loses t5e file and releases an# reso!rces associated wit5 t5e c!rrent file stream. +reates an o"?ect t5at contains all t5e rele,ant information reD!ired to generate a ro1# !sed to comm!nicate wit5 a remote o"?ect. -,erridden. Waits for t5e ending as#nc5rono!s read to com lete. -,erridden. Ends an as#nc5rono!s write, "locAing !ntil t5e I<- o eration 5as com leted. -,erloaded. Determines w5et5er two -"?ect instances are eD!al. -,erridden. +lears all "!ffers for t5is stream and ca!ses an# "!ffered data to "e written to t5e !nderl#ing de,ice. Ser,es as a 5as5 f!nction for a artic!lar t# e, s!ita"le for !se in 5as5ing algorit5ms and data str!ct!res liAe a

V. Matos - Input/Output VB.NET Page 0

9et)ifetimeSer,ice (in5erited from Mar(6alB1Ref4b<ect) 9etT# e (in5erited from 4b<ect) InitialiPe)ifetimeSer,ice (in5erited from Mar(6alB1Ref4b<ect) )ocA Read Read=#te SeeA Set)engt5 ToString (in5erited from 4b<ect) InlocA Write Write=#te Protected Met6od(

5as5 ta"le. Retrie,es t5e c!rrent lifetime ser,ice o"?ect t5at controls t5e lifetime olic# for t5is instance. 9ets t5e T# e of t5e c!rrent instance. -"tains a lifetime ser,ice o"?ect to control t5e lifetime olic# for t5is instance. Pre,ents access "# ot5er rocesses to all or art of a file. -,erridden. Reads a "locA of "#tes from t5e stream and writes t5e data in a gi,en "!ffer. -,erridden. Reads a "#te from t5e file and ad,ances t5e read osition one "#te. -,erridden. Sets t5e c!rrent osition of t5is stream to t5e gi,en ,al!e. -,erridden. Sets t5e lengt5 of t5is stream to t5e gi,en ,al!e. Ret!rns a String t5at re resents t5e c!rrent -"?ect. Allows access "# ot5er rocesses to all or art of a file t5at was re,io!sl# locAed. -,erridden. Writes a "locA of "#tes to t5is stream !sing data from a "!ffer. -,erridden. Writes a "#te to t5e c!rrent osition in t5e file stream.

Allocates a Wait@andle o"?ect. +reateWait@andle (in5erited from Stream) Dis ose Releases t5e !nmanaged reso!rces !sed "# t5e FileStream and o tionall# releases t5e managed reso!rces. -,erridden. Ens!res t5at reso!rces are freed and ot5er clean! o erations are erformed w5en t5e gar"age collector reclaims t5e FileStream. In +3 and +QQ, finaliPers are e1 ressed !sing destr!ctor s#nta1. +reates a s5allow co # of t5e c!rrent -"?ect. 7em"erwise+lone (in5erited from 4b<ect)

0inaliPe

V. Matos - Input/Output VB.NET Page 1

You might also like