files vb6 net

Upload: rkkv1234

Post on 04-Jun-2018

248 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Files Vb6 Net

    1/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 simle files.

    Module: modFile

    P!lic T#e empRecord $ Define !ser-definedt#e.

    ID As Integer %ame As String & 'End T#e

    Form: frmFile

    Private Sub cmdClearClic!"# ist*.+learEnd S!

    Private Sub cmdMa!eFileClic!"#$ Declare ariales.Dim m#Record As emRecordDim Record%!mer As Integer

    $ en file for random access.en "c/TEST0IE.t1t" 0or Random 2

    As 3* en 4 en(m#Record)

    $P!t some d!mm# data into t5e file0or Record%!mer 4 * To 6 $ oo 6 times. m#Record.ID 4 Record%!mer $ Define ID. m#Record.%ame 4 "7#%ame" 8 2

    Record%!mer $ +reate a string. $ Write record to file. P!t 3*, Record%!mer, m#Record

    $ S5ow t5e record into t5e listo1 ist*.AddItem m#Record.ID 8 " " 8 2

    m#Record.%ame%e1t Record%!mer+lose 3* $ +lose file.

    End S!

    Private Sub cmdRa$domReadClic!"#n Error 9oTo Prolems Dim m#Record As emRecord Position 4 :al(t1tPosition)

    If Position ; 0(*) < en(m#Record) T5en 9oTo Prolems End If

    9et 3*, Position, m#Record $ Read t5irdrecord. t1t%ame 4 m#Record.%ame 9oTo Adios

    Prolems 7sg=o1 "=ad Inde1 - tr# Again"AdiosEnd S!

    Private Sub cmdReadFileClic!"#$ Declare ariales.

    Dim m#Record As emRecordDim Position, totalRecords, #tes0ile,#tesRecord$ en samle file for random access.en "c/TEST0IE.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 samle file !sing t5e 9et.

    9et 3*, Position, m#Record $ Read t5e record $ S5ow t5e record into t5e listo1 ist*.AddItem m#Record.ID 8 " " 8 2 m#Record.%ame %e1t $+lose 3* $ +lose t5e actie file.End S!

    Private Sub FormLoad"#n Error 9oTo Adios$delete an# reio!s ersion of t5e file

    >ill "c/TEST0IE.t1t"AdiosEnd S!

  • 8/13/2019 Files Vb6 Net

    2/21

    VB%&E' ver(io$

    empRecord Cla((

    P!licStr!ct!reemRecord$ Define !ser-defined t#e. DimID AsS5ort )VBFi*edStri$+",#. Dim%ame AsStringEndStr!ct!re

    frmFile Cla(( "/I0#

    PriateS!cmdClearClic!(=#:aleentSender AsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs)@andlescmd+lear.+lic ist*.Items.+lear()EndS!

    PriateS!cmdMa!eFileClic!(=#:aleentSenderAsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs) @andlescmd7ae0ile.+lic

    $ Declare ariales.Dimm#Record AsemRecordDimRecord%!mer AsS5ortDimrecen AsInteger$ en file for random access.recen 4 en(m#Record)

    0ileen(*, "c/TEST0IE.t1t",en7ode.Random, enAccess.Write,enS5are.ocWrite, recen)

    $P!t some d!mm# data into t5e file0orRecord%!mer 4 * To6 $ oo 6 times. $ Define ID. m#Record.ID 4 Record%!mer

    $ +reate a string. m#Record.%ame 4 "Data-Emlo#ee-" 8

    Record%!mer$ Write record to file.0ileP!t(*, m#Record, Record%!mer)$ S5ow t5e record into t5e listo1

    ist*.Items.Add(m#Record.ID 8 2 " " 8 m#Record.%ame)%e1tRecord%!mer

    0ile+lose(*) $ +lose file.

    $enale t5e ot5er command !ttons cmdRead0ile.Enaled 4 Tr!e 7e.cmdRandomRead.Enaled 4 Tr!eEndS!

    PriateS!cmdRa$domReadClic!(=#:aleentSender AsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs) @andlescmdRandomRead.+lic Dimm#Record AsemRecord DimPosition AsS5ort Dimfile%!m AsInteger $ en file (if needed) for random access. nErrorRes!me%e1t file%!m 4 0ree0ile() 0ileen(file%!m, "c/TEST0IE.t1t",

    en7ode.Random, , , en(m#Record))

    nError9oToProlems Position 4 :al(t1tPosition.Te1t) IfPosition ; 0(file%!m) < en(m#Record) T5en 9oToProlems EndIf 0ile9et(file%!m, m#Record, Position)

    t1t%ame.Te1t 4 m#Record.%ame 9oToAdiosProlems 7sg=o1("=ad Inde1 - tr# Again")Adios 0ile+lose(file%!m) EndS!

    PriateS!cmdReadFileClic!(=#:aleentSender AsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs)@andles cmdRead0ile.+lic $ Declare ariales. Dimm#Record AsemRecord Dim#tes0ile, Position, AsS5ort DimtotalRecords, recen AsS5ort Dimfile%!m AsInteger

    $ en samle file for random access. file%!m 4 0ree0ile() recen 4 en(m#Record) 0ileen(file%!m, "c/TEST0IE.t1t",

    en7ode.Random, , , recen) $find o!t 5ow man# records are t5ere in file 3* #tes0ile 4 0(file%!m) totalRecords 4 #tes0ile < recen $ read data from last rec. to first rec. 0orPosition 4 totalRecords To* Ste-* 0ile9et(file%!m, m#Record, Position)

    $ S5ow t5e record into t5e listo1 ist*.Items.Add(m#Record.ID 8 " "

    8 m#Record.%ame)%e1t

    0ile+lose(file%!m) $ +lose file. EndS!

    PriateS!frmFileLoad(=#:aleentSender AsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs)@andles7#=ase.oad nError9oToAdios

    >ill("c/TEST0IE.t1t")

    AdiosEndS!

  • 8/13/2019 Files Vb6 Net

    3/21

    VB6 S1$ta*% /E' 2 P0' S'3'EME&'

    Readse#word secif#ing t5e oerations ermitted on t5e oen file

    # ot5er rocesses Sared, Loc! Read, Loc! 7rite, and Loc!

    Read 7rite.filen!mer Re!ired. A alid file n!mer in t5e range * to H**, incl!sie. se t5e

    FreeFilef!nction to otain t5e ne1t aailale file n!mer.reclengt5 tional. %!mer less t5an or e!al to J',6K6 (#tes). 0or files

    oened for random access, t5is al!e is t5e record lengt5. 0orse!ential files, t5is al!e is t5e n!mer of c5aracters !ffered.

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

  • 8/13/2019 Files Vb6 Net

    4/21

    Visual Basic .NET I/OT5ese e1certs are taen

    from t5e

    L7icrosoft .%ET

    @el 0acilit#.

    File Access Types, Functions, and Statements

    =# itself, a file is not5ing more t5an a series of related #tes of data on a dis. W5en #o!r alication accesses afile, it m!st ass!me w5et5er t5e #tes reresent c5aracters, data records, integers, strings, and so on. o! tell#o!r alication w5at to ass!me # secif#ing t5e file$s access t#e.

    T5e file access t#e #o! !se deends on w5at ind of data t5e file contains. :is!al =asic roides t5ree t#es of fileaccess

    Se!ential, for reading and writing te1t files in contin!o!s locs.

    Random, for reading and writing te1t or inar# files str!ct!red as fi1ed-lengt5 records.

    =inar#, for reading and writing aritraril# str!ct!red files.

    Sequential accessis designed for !se wit5 lain te1t files. Eac5 c5aracter in t5e file is ass!med to reresent eit5er

    a te1t c5aracter or a te1t-formatting se!ence, s!c5 as a newline c5aracter. Data is stored as A%SI c5aracters.

    A file oened for random accessis ass!med to e comosed of a set of identical-lengt5 records, or sets of fieldscontaining information. o! can emlo# !ser-defined t#es to create records made ! of n!mero!s fields, eac5 ofw5ic5 can 5ae different data t#es. Data is stored as inar# information.

    Binary accessallows #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 e1cet t5at no ass!mtions are made ao!t data t#e or recordlengt5. @oweer, #o! m!st now recisel# 5ow t5e data was written to t5e file to retriee it correctl#. 0or e1amle,if #o! store a series of names and 5one n!mers, #o! need to rememer t5at t5e first field (t5e name) is te1t and

    t5e second field (t5e 5one n!mer) is n!meric.

    File 3cce(( Fu$ctio$( a$d Stateme$t(

    T5e following f!nctions are !sed wit5 se!ential, random, and inar# file access.

    Dir 0!nction E0 0!nction 0ile+o# 0!nction

    0ileDateTime 0!nction 0ileen 0!nction 0ree0ile 0!nction

    9etAttr 0!nction oc 0!nction 0 0!nction

    See 0!nction SetAttr 0!nction

    Woring wit5 large amo!nts of data often re!ires t5at data e written to or read from a file. =efore eit5eroeration can tae lace, 5oweer, t5e file m!st first e oened. T5e 0ileen 0!nction lets #o! create and accessfiles wit5 one of t5ree t#es of file access

    Se!ential access (I$put, 4utput, and 3ppe$dmodes) is !sed for writing te1t files, s!c5 as error logs

    and reorts. Random access (Ra$dommode) is !sed to read and write data to a file wit5o!t closing it. Random-access

    files ee data in records, w5ic5 maes it eas# to locate information !icl#.

    =inar# access (Bi$ar1mode) is !sed to read or write to an# #te osition in a file, s!c5 as storing or

    disla#ing a itma image.

    Do not !se t5e File4pe$f!nction to mani!late files t5at are !s!all# edited # a secific alication, s!c5as 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

  • 8/13/2019 Files Vb6 Net

    5/21

    T5e following tale s5ows t5e statements t#icall# !sed w5en writing data to and reading it from files.

    3cce(( t1pe 7riti$+ data Readi$+ data

    Se!ential Print, Printine 0!nctions In!tString 0!nction

    Random 0ileP!t 0!nction 0ile9et 0!nction

    =inar# 0ileP!t 0!nction 0ile9et 0!nction

    FileOpen Function

    ens a file for in!t or o!t!t.

    Public Sub File4pe$" B1ValFileNumber3( I$te+er5 B1ValFileName3( Stri$+5

    B1Val Mode3( 4pe$Mode5 4ptio$al B1ValAccess3( 4pe$3cce(( 8 4pe$3cce((%Default5 4ptio$al B1ValShare3( 4pe$Sare 8 4pe$Sare%Default5 4ptio$al B1ValRecordLength3( I$te+er 8 -9 #

    Parameter(

    FileNumberRe!ired. An# alid file n!mer. se t5e FreeFilef!nction to otain t5e ne1t aailale file n!mer.

    FileNameRe!ired. Stri$+e1ression t5at secifies a file name M ma# incl!de director# or folder, and drie.

    ModeRe!ired. E$umsecif#ing t5e file mode 3ppe$d, Bi$ar1, I$put, 4utput, or Ra$dom.

    Accesstional. >e#word secif#ing t5e oerations ermitted on t5e oen file Read, 7rite, or Read7rite.

    Defa!lts to Read7rite.Share

    tional. E$umsecif#ing t5e oerations restricted on t5e oen file # ot5er rocesses Sared, Loc!Read, Loc! 7rite, and Loc! Read 7rite. Defa!lts to Sared.

    RecordLengthtional. %!mer less t5an or e!al to J',6K6 (#tes). 0or files oened for random access, t5is al!e is

    t5e record lengt5. 0or se!ential files, t5is al!e is t5e n!mer of c5aracters !ffered.

    E*ceptio$(2Error(

    E*ceptio$ t1pe Error $um% Co$ditio$

    Arg!mentE1cetion H Inalid 3cce((5 Sare5 orMode%

    Arg!mentE1cetion H 7rite4$l1file is oened for I$put%

    Arg!mentE1cetion H Read4$l1file is oened for 4utput%

    Arg!mentE1cetion H Read4$l1file is oened for 3ppe$d%

    Arg!mentE1cetion H Record lengt5 is negatie (and not e!al to -*).

    IE1cetion H' FileNumberis inalid (N-* or ;'HH), or FileNumberis alread# in !se.

    IE1cetion HH FileNameis alread# oen, or FileNameis inalid.

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

  • 8/13/2019 Files Vb6 Net

    6/21

    Remar!(

    o! m!st oen a file efore an# I

  • 8/13/2019 Files Vb6 Net

    7/21

    Opening Files for Seuential Access

    sing S1(tem%I4 is t5e recommended wa# to create te1t files, !t s5o!ld #o! need to !se older te1t file-creationmet5ods, t5is section s5ows #o! 5ow.

    Se!ential access wors est w5en #o! want to rocess files consisting onl# of te1t, s!c5 as files created wit5 at#ical te1t editor, not files in w5ic5 data is diided into a series of records. Se!ential access is not well s!ited forstoring long series of n!mers, eca!se eac5 n!mer is stored as a c5aracter string. T5!s, a fo!r-digit n!merwo!ld re!ire fo!r #tes of storage instead of t5e two #tes re!ired to store t5e same n!mer as an integer.

    W5en #o! oen a file for se!ential access, #o! m!st secif# w5et5er to in!t c5aracters from t5e file (I$put

    mode), o!t!t c5aracters to t5e file (4utputmode), or aend c5aracters to t5e file (3ppe$dmode).

    'o ope$ a file for (eue$tial acce((

    se t5e 0ileen 0!nction wit5 t5e following s#nta1

    File4pe$(FileNumber, FileName, 4pe$Mode.I$put)

    W5en #o! oen a se!ential file for I$put, t5e file m!st alread# e1istO ot5erwise an error occ!rs. W5en #o! tr# tooen a none1istent file for 4utputor 3ppe$d, 5oweer, t5e File4pe$statement creates t5e file first and t5enoens it.

    After oening a file for an I$put, 4utput, or 3ppe$doeration, #o! m!st close it wit5 t5e 0ile+lose 0!nction

    statement efore reoening it for anot5er t#e of oeration.

    Editing Files Opened for Seuential Access

    To edit a file, #o! m!st first read its contents to rogram ariales, t5en c5ange t5e ariales, and finall#, writet5e ariales ac to t5e file. T5e following sections disc!ss 5ow to edit records oened for se!ential access.

    'o read (tri$+( from file(

    *. Retriee t5e contents of a te1t file # oening it for I$put.'. se t5e ineIn!t, In!tString, or In!tf!nctions to co# t5e file into rogram ariales.

    :is!al =asic roides statements and f!nctions t5at read and write se!ential files one c5aracter at a time or oneline at a time. 0or e1amle, t5e following code fragment reads a file line # line

    Dim ines0rom0ile, %e1tine As String, 0ile%!m As IntegerDo ntil E0(0ile%!m)

    ineIn!t(0ile%!m, %e1tine) ines0rom0ile 4 ines0rom0ile 8 %e1tine 8 +5r(*J) 8 +5r(*)oo

    Alt5o!g5 t5e Li$eI$putf!nction recognies t5e end of a line w5en it comes to t5e carriage ret!rn

  • 8/13/2019 Files Vb6 Net

    8/21

    ines0rom0ile 4 In!tString(0ile%!m, +5ar+o!nt)

    o! can also !se t5e I$putf!nction, w5ic5 reads a list of n!mers and

  • 8/13/2019 Files Vb6 Net

    9/21

    #andom File Access

    T5e #tes in random-access files form records of !niform lengt5, eac5 containing one or more fields. A record wit5one field corresonds to an# standard t#e, s!c5 as an integer or fi1ed-lengt5 string. A record wit5 more t5an one

    field corresonds to a !ser-defined t#e.

    In t5is section #o! declare, oen, edit, and write to a random-access file, !sing a 5#ot5etical Emlo#ee Recordsdataase as an e1amle. Eac5 file-access asect of t5e e1amle is disc!ssed searatel# in t5e toics listedimmediatel# elow.

    $eclaring Varia%les for #andom File Access

    =efore #o!r alication oens a file for random access, it s5o!ld declare all ariales re!ired to 5andle data fromt5e file. T5is incl!des !ser-defined t#es, w5ic5 corresond to records in t5e file, as well as standard t#es forot5er ariales t5at 5old data related to rocessing a random-access file.

    =efore oening a file for random access, define a str!ct!re t5at corresonds to t5e records t5e file contains or will

    contain. 0or e1amle, a 5#ot5etical Emlo#ee Records dataase mig5t contain a !ser-defined data t#e calledPerson as follows

    Str!ct!re Person P!lic ID As Integer

    P!lic 7ont5l#Salar# As Decimal P!lic astReiewDate 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 Reiew+omments As StringEnd Str!ct!re

    $eclaring Field Varia%les in a Structure $efinition

    =eca!se all records in a random-access file m!st 5ae t5e same lengt5, it is often !sef!l for string elements in a!ser-defined t#e to 5ae a fi1ed lengt5 also. In t5e Person t#e declaration aoe, for e1amle, 0irst%ame andast%ame 5ae a fi1ed lengt5 of *H c5aracters. To declare a fi1ed-lengt5 string, set its lengt5 wit5 t5eVBFi*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 saces in t5e record wit5 lans (c5aracter code J'). If t5e string is longer t5an t5efield sie, :is!al =asic tr!ncates it. (If #o! !se ariale-lengt5 strings, t5e total sie of an# record stored wit5 t5e0ileP!t 0!nction or retrieed wit5 t5e 0ile9et 0!nction m!st not e1ceed t5e record lengt5 secified in t5e 0ileen

    0!nction).

    After defining a str!ct!re t5at corresonds to a t#ical record, declare an# ot5er ariales t5at #o!r alicationneeds to rocess a file oened for random access. T5e Emlo#ee Records dataase, for instance, declaresEmlo#ee, Position, and astRecord ariales, as follows

    $ Declare a record ariale.P!lic Emlo#ee As Person$ Trac t5e c!rrent record.P!lic Position As ong

    $ 9et t5e n!mer of t5e last record in t5e file.P!lic astRecord As ong

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

  • 8/13/2019 Files Vb6 Net

    10/21

    Opening Files for #andom Access

    After #o! create t5e file, #o! need to oen it to mae an# modifications.

    'o ope$ a file for ra$dom acce((

    se t5e 0ileen 0!nction wit5 t5e following s#nta1

    File4pe$(FileNumber, FileName, 4pe$Mode.Ra$dom, , , RecordLength)

    FileNumberand FileNamesecif# t5e file n!mer and name of t5e file to e oened, resectiel#. RecordLengthsecifies t5e sie of eac5 record in #tes. If RecordLength is less t5an t5e act!al lengt5 of t5e record written to t5efile, an error is generated. If RecordLength is greater t5an t5e act!al lengt5 of t5e record, t5e record is written,alt5o!g5 some dis sace ma# e wasted. %ote t5at eer# Stri$+ariale in :is!al =asic stores an AS+II string,and t5at #o! m!st secif# t5e #te lengt5 of t5at AS+II string.

    In Declaring :ariales for Random 0ile Access, a 5#ot5etical Emlo#ee Records dataase first defined a Person

    data t#e and t5en declared a record ariale of t5at t#e as well as two ot5er ariales for rocessing records.T5e following code contin!es t5e Emlo#ee Records e1amle, demonstrating 5ow to oen a random-access file to

    accet Emlo#ee data of !ser-defined t#e Person

    S! 7ain() Dim 0ile%!m As Integer, Recengt5 As ong, Emlo#ee As Person $ +alc!late t5e record lengt5. Recengt5 4 en(Emlo#ee) $ 9et t5e ne1t aailale file n!mer. 0ile%!m 4 0ree0ile $ en t5e new file wit5 t5e 0ileen statement.

    0ileen(0ile%!m, "70IE.DAT", en7ode.Random, , , Recengt5)End S!

    Editing Files Opened for #andom Access

    To edit a random-access file, first read records from t5e file into rogram ariales, and t5en c5ange t5e al!es int5e ariales. In Declaring :ariales for Random 0ile Accessand ening 0iles for Random Access,a 5#ot5eticalEmlo#ee Records dataase defined ariales for reading and writing emlo#ee records. T5e following stedescries 5ow to read and co# emlo#ee records !sing t5ose ariales.

    'o read a$d cop1 record( i$to variable(

    se t5e 0ile9et?ect 7et5od, w5ic5 coies a record from t5e Emlo#ee Records file into t5e Emlo#ee

    ariale

    0ile9et(0ile%!m, Emlo#ee, Position)

    In t5is line of code, FileNumcontains t5e n!mer t5at t5e 0ileen 0!nction !sed to oen t5e file,

    Positioncontains t5e n!mer of t5e record to co#, and Employee, declared as t5e !ser-defined

    t#e Person, receies t5e contents of t5e record.

    !riting Varia%les to #ecords

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

    http://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vacondeclaringvariablesforrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vacondeclaringvariablesforrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vacondeclaringvariablesforrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vaconopeningfilesforrandomaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vaconopeningfilesforrandomaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vacondeclaringvariablesforrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vacondeclaringvariablesforrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vaconopeningfilesforrandomaccess.htm
  • 8/13/2019 Files Vb6 Net

    11/21

    After editing records read into rogram ariales from a random-access file, !se t5e 0ileP!t 0!nction to relace oradd records. InDeclaring :ariales for Random 0ile Accessand ening 0iles for Random Access, a 5#ot5eticalEmlo#ee Records dataase defined ariales for reading and writing emlo#ee records. T5e following stesdescrie 5ow to relace and add emlo#ee records !sing t5ose ariales.

    'o replace a record

    se FilePutto secif# t5e osition of t5e record #o! want to relace. T5e Random 0ile Accesse1amle

    ill!strates t5is in t5e following code

    0ileP!t(0ile%!m, Emlo#ee, Position)

    T5is code relaces t5e record n!mer secified # Positionwit5 t5e data in t5e Employeeariale.

    'o add a record

    sing FilePut, set t5e al!e of t5e Position ariale e!al to one more t5an t5e n!mer of records in t5e

    file. To add a record to a file t5at contains fie records, for e1amle, set Position e!al to K.

    In t5e case of t5e Emlo#ee Records e1amle, s!stit!ting t5e following statements for t5e FilePutstatement aoe adds a record to t5e end of t5e file instead of oerwriting t5e one secified # Position

    astRecord 4 astRecord Q *0ileP!t(0ile%!m, Emlo#ee, astRecord)

    Deleti$+ Record(

    o! 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 emt# records in #o!r file eca!se t5e# waste sace. To aoid t5is, #o! can co# t5e remaining recordsto a new file, and t5en delete t5e old oneO or, #o! can mae a note of t5e location of t5e emt# record and !set5at slot on t5e ne1t insert oeration.

    'o delete a record

    +lear t5e record$s fields.

    'o remove a deleted record

    *. +reate a new file.'. +o# all t5e alid records from t5e original file into t5e new file.J. +lose t5e original file and !se t5e =illf!nction to delete it.F. se t5e Re$amef!nction to rename t5e new file wit5 t5e name of t5e original file.

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

    http://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vacondeclaringvariablesforrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vacondeclaringvariablesforrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vaconopeningfilesforrandomaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vbconrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vbconrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vacondeclaringvariablesforrandomfileaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vaconopeningfilesforrandomaccess.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vbcn7/html/vbconrandomfileaccess.htm
  • 8/13/2019 Files Vb6 Net

    12/21

    FileLe$ Fu$ctio$

    Ret!rns a Lo$+al!e secif#ing t5e lengt5 of a file in #tes.

    Public Fu$ctio$ FileLe$"B1Val PathName3( Stri$+# 3( Lo$+

    Parameter

    PathNameRe!ired. Stri$+e1ression t5at secifies a file. PathNamema# incl!de t5e director# or folder, and t5e

    drie.

    E*ceptio$(2Error(

    E*ceptio$ t1pe Error $umber Co$ditio$

    0ile%ot0o!ndE1cetion HJ 0ile does not e1ist.

    Remar!(

    If t5e secified file is oen w5en t5e FileLe$f!nction is called, t5e al!e ret!rned reresents t5e sie of t5e file at

    t5e time it was oened.

    &ote To otain t5e c!rrent lengt5 of an oen file, !se t5e L4Ff!nction.

    E*ample

    T5is e1amle !ses t5e FileLe$f!nction to ret!rn t5e lengt5 of a file in #tes. 0or !roses of t5is e1amle,ass!me t5at TEST0IE is a file containing some data.

    Dim 7#Sie As ong7#Sie 4 FileLe$""TEST0IE"# $ Ret!rns file lengt5 (#tes).

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

    http://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vblr7/html/vaerrfilenotfound.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vblr7/html/vaerrfilenotfound.htm
  • 8/13/2019 Files Vb6 Net

    13/21

    L4F Fu$ctio$

    Ret!rns a Lo$+reresenting t5e sie, in #tes, of a file oened !sing t5e File4pe$f!nction.

    Public Fu$ctio$ L4F"B1Val FileNumber3( I$te+er# 3( Lo$+

    Parameter

    FileNumberRe!ired. An I$te+er containing a alid file n!mer.

    E*ceptio$(2Error(

    E*ceptio$ t1pe Error $umber Co$ditio$

    IE1cetion H' FileNumberdoes not e1ist.

    IE1cetion HF 0ile mode is inalid.

    Remar!(

    se t5e FileLe$f!nction to otain t5e lengt5 of a file t5at is not oen.

    E*ample

    T5is e1amle !ses t5e L4Ff!nction to determine t5e sie of an oen file. T5is e1amle ass!mes t5at TEST0IE is a

    te1t file containing samle data.

    Dim lengt5 As Integer0ileen(*, "+/TEST0IE.TT", en7ode.In!t) $ en file.lengt5 4 L4F"*# $ 9et lengt5 of file.De!g.Writeine(lengt5)0ile+lose(*) $ +lose file.

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

    http://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vblr7/html/vaerrbadfilenameornumber.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vblr7/html/vaerrbadfilemode.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vblr7/html/vaerrbadfilenameornumber.htmhttp://ms-help//MS.VSCC/MS.MSDNQTR.2002JAN.1033/vblr7/html/vaerrbadfilemode.htm
  • 8/13/2019 Files Vb6 Net

    14/21

    Binary File Access

    se inar# access w5en it is imortant to ee file sie small. =eca!se inar# access doesn$t re!ire fi1ed-lengt5fields, t5e t#e declaration can omit t5e string lengt5 arameters. T5is enales #o! to consere dis sace #

    !ilding ariale-lengt5 records.

    4pe$i$+ a File for Bi$ar1 3cce((

    se inar# access w5en it is imortant to ee file sie small. =eca!se inar# access doesn$t re!ire fi1ed-lengt5fields, #o! can consere dis sace # !ilding ariale-lengt5 records.

    'o ope$ a file for bi$ar1 acce((

    se t5e 0ileen 0!nction wit5 t5e following s#nta1

    File4pe$(FileNumber, FileName, 4pe$Mode.Bi$ar1)

    As #o! can see, oening files for inar# access differs from ening 0iles for Random Accessin t5at t5eRecordLengthe1ression is not secified. (If #o! do incl!de a record lengt5 in a inar#-access File4pe$f!nction,it is ignored.)

    To areciate inar# access, consider t5e Emlo#ee Records e1amle disc!ssed in t5e Random 0ile Accesssection.T5is random-access file !ses fi1ed-lengt5 records and fields to store information ao!t emlo#ees. As a res!lt,eer# record in t5e Emlo#ee Records dataase taes t5e same n!mer of #tes, regardless of t5e act!al contentsof t5e fields.

    o! can minimie t5e sie of Emlo#ee Records # !sing inar# access. =eca!se #o! are not re!ired to !se fi1ed-lengt5 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 astReiewDate As ong 0irst%ame As String ast%ame As String Title As String Reiew+omments As StringEnd Str!ct!re

    P!lic Emlo#ee As Person $ Defines a record.

    Eac5 emlo#ee record in t5e Emlo#ee Records file now occ!ies onl# t5e e1act n!mer of #tes re!ired, eca!set5e fields are of ariale lengt5s.

    T5e drawac to inar# in!t

  • 8/13/2019 Files Vb6 Net

    15/21

    Accessing Files &it' FileSystemO%(ect

    T5e 0ile S#stem ?ect (0S) model roides an o?ect-ased tool for woring wit5 folders and files. It allows #o!to !se t5e familiar object.methods#nta1 wit5 a ric5 set of roerties, met5ods, and eents to rocess folders and

    files. o! can also emlo# t5e traditional :is!al =asic statements and commands.

    T5e 0S model gies #o!r alication t5e ailit# to create, alter, moe, and delete folders, or to determine if andw5ere artic!lar folders e1ist. It also enales #o! to get information ao!t folders, s!c5 as t5eir names and t5edate t5e# were created or last modified.

    T5e 0S model maes rocessing files m!c5 easier as well. W5en rocessing files, #o!r rimar# goal is to storedata in an efficient, eas#-to-access format. o! need to e ale to create files, insert and c5ange t5e data, and

    o!t!t (read) t5e data. Alt5o!g5 #o! can store data in a dataase, doing so adds a significant amo!nt of oer5eadto #o!r alication. o! ma# not want to 5ae s!c5 oer5ead, or #o!r data access re!irements ma# not call fort5e e1tra f!nctionalit# associated wit5 a f!ll-feat!red dataase. In t5is case, storing #o!r data in a te1t file orinar# file is t5e most efficient sol!tion.

    T5e 0S model, contained in t5e Scriting t#e lirar# (Scrr!n.dll), s!orts t5e creation and mani!lation of te1t

    files t5ro!g5 t5e 'e*tStreamo?ectO 5oweer, t5e 0S model does not s!ort inar# files. To mani!late inar#files, !se t5e 0ileen 0!nction wit5 t5e Bi$ar1e#word.

    T5e following o?ects mae ! t5e 0S model

    4b

  • 8/13/2019 Files Vb6 Net

    16/21

    *. n t5e Pro

  • 8/13/2019 Files Vb6 Net

    17/21

    Drive'1pe T#e of drie (remoale, fi1ed, networ, +D-R7, orRA7 dis)

    Serial&umber Drie$s serial n!mer

    FileS1(tem T#e of file s#stem t5e drie !ses (0AT, 0ATJ', or %T0S)

    I(Read1 W5et5er a drie is aailale for !se

    Sare&ame,Volume&ame %ame of t5e s5are and

  • 8/13/2019 Files Vb6 Net

    18/21

    Se!ential te1t files (sometimes referred to as a tet stream) are !sef!l w5en #o! want to read t5e contents of afile wit5in a FileS1(tem4b

  • 8/13/2019 Files Vb6 Net

    19/21

    Internal=!ffererflowE1cetion T5e e1cetion t5rown w5en t5e internal !ffer oerflows.

    IDescritionAttri!te Sets t5e descrition is!al designers can disla# w5enreferencing an eent, e1tender, or roert#.

    IE1cetion T5e e1cetion t5at is t5rown w5en an I

  • 8/13/2019 Files Vb6 Net

    20/21

    FileStream Member(

    Public Co$(tructor(

    0ileStream +onstr!ctorerloaded. Initialies a new instance of t5e FileStreamclass.

    Public Propertie(

    +anRead

    erridden. 9ets a al!e indicating w5et5er t5e c!rrentstream s!orts reading.

    +anSee

    erridden. 9ets a al!e indicating w5et5er t5e c!rrentstream s!orts seeing.

    +anWrite

    erridden. 9ets a al!e indicating w5et5er t5e c!rrentstream s!orts writing.

    @andle

    9ets t5e oerating s#stem file 5andle for t5e file t5at t5e

    c!rrent FileStreamo?ect encas!lates.

    IsAs#nc

    9ets a al!e indicating w5et5er t5e FileStreamwasoened as#nc5rono!sl# or s#nc5rono!sl#.

    engt5

    erridden. 9ets t5e lengt5 in #tes of t5e stream.

    %ame

    9ets t5e name of t5e FileStreamt5at was assed to t5econstr!ctor.

    Position

    erridden. 9ets or sets t5e c!rrent osition of t5isstream.

    Public Metod(

    =eginRead

    erridden. =egins an as#nc5rono!s read.

    =eginWrite

    erridden. =egins an as#nc5rono!s write.

    +lose

    erridden. +loses t5e file and releases an# reso!rcesassociated wit5 t5e c!rrent file stream.

    +reate?Ref(in5erited fromMar(alB1Ref4b

  • 8/13/2019 Files Vb6 Net

    21/21

    5as5 tale.

    9etifetimeSerice (in5erited fromMar(alB1Ref4b