资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 编程开发 >
Advertisement
文章正文

Utilize the full functionality of the Whidbey File Management_编程

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   
  • 一个ASP统计制作实例 
  • 用ASP实现网上“五子棋”大赛 
  • vbscript错误代码及对应解释大全 
  • 如何学好ASP? 
  • VBScript教程 第五课 VBScript常数 
  • VBScript教程 第三课 VBScript数据类型 
  • VBScript教程 第一课 什么是 VBScript 
  • VBScript教程 第二课 在HTML页面中添加VBscript代码 
  • Web设计里的软件工程思想 
  • 用InstallShield进行ASP软件的打包和自动安装 
  • 页面导航:

    正文内容:
    this article discusses:
    • file system management and its uses
    • file permissions
    • file modes, file access, file shares

    this article uses the following technologies: vb.net and windows forms

    file management in whidbey/vb.net the beating heart of the technologies that comprise windows forms. this article explains various file management operations such as read or write, retrieving properties or setting the properties or copy or move a file using the whidbey/visual studio.net environment. i hope this will helpful to understand the operation of managing a file.

    system.io.namespace

    the input/output operation of file management is handled by the classes in the system.io.namespace. the system.io.namespace supports the following activities:

      reading and writing to binary files, text files etc.
      reading and writing to data streams
      basic file support
      directory support
      memory streams
      network streams

    you have to include this namespace to your project.

    classes that supports system.io.namespace

    the following classes will support system.io.namespace in vb.net.

    1. the class binary reader used for reads primitive data types as binary values. primitive data types are those that are not defined in terms of other data types. because primitive data types are the basis for all other types, they cannot have element content or attributes. examples of primitive data types are string, float, decimal, anyuri, and qname.

    usage:

    dim objbinreader as new binaryreader(file.open _ (“c:\mysamplefile.txt”, filemode.open))

    2. the class binarywriter used for writes primitive data types as binary values to a stream.

    usage: 

    dim objbinwriter as new binarywriter(file.open _ (“c:\mysamplefile.txt”, filemode.create))

    3. the class bufferedstream used for buffering to another stream for read and write operations.

    usage: 

    ‘ create an networkstream object “objnetstream” that owns clientsocket dim objnetstream as new networkstream(clientsocket, true) ‘ create an bufferedstream object ‘ “objbufstream” on top of the networkstream dim objbufstream as new _ bufferedstream(objnetstream, _ intstreambuffersize)

    note: the networkstream class provides methods for sending and receiving data over stream sockets in blocking mode

    4. the class directory used for creating, copying, moving, deleting and renaming directories and sub directories. the directory class includes the system.io namespace.

    some of the public methods used for directory class is described as follows.

    o createdirectory – creates a new directory in a specified path.
    o delete - delete the directory from the specified path
    o exists – checks whether the associated directories exists in the specific path.
    o getcreationtime – get the creation time of directory
    o getcreationtimeutc – get the creation time in universal coordinated time
    o getcurrentdirectory – get the current working directory of the application
    o getdirectories – gets the name of subdirectories in the working folder
    o getdirectoryroot – get root and volume information for the specified path
    o getfiles - get or returns all the files listed in the specified directory.
    o getfilesytementries - get or returns all the files and sub directories listed in the specified
      directory.
    o getlastaccesstime – last access date and time of the specified directory
    o getlastaccesstimeutc – last access date and time of the specified directory in universal
      coordinator time (utc) format.
    o getlastwritetime – last written date and time of a specified directory.
    o getlastwritetimeutc – last written date and time of a specified directory in universal
      coordinator time (utc).
    o getlogicaldrives – get all the logical drives on the computer.
    o getparent – get the absolute and relative path of the parent directory.
    o move – move the specific directory to another location
    o setcreationtime – sets the creation date and time for the specified directory or file.
    o setcreationtimeutc – sets the creation date and time for the specified directory or file in
      universal coordinated time format.
    o setcurrentdirectory – sets the current directory to a specified directory in an application path.
    o setlastaccesstime - sets the last access date and time for the specified directory or file.

    usage:

    ‘ checking whether the directory exists in the specified path dim strmypath as string = “c:\mysample” if directory.exists(strmypath) = true then directory.delete(strmypath) else directory.createdirectory (strmypath) end if

    5. the class directoryinfo used the same functionality as directory class except if you are not going to reuse an
    object several times.

    6. the class directorynotfoundexception throws exception error when the file or directory not found in the
    specified path.


    7. the class endofstreamexception throws exception error at the past the end of a stream.

    8. the class erroreventargs provides data for error event.

    usage:
     

    ‘ checking whether the directory exists in the specified path dim strmypath as string = “c:\mysample” ‘ initializes the “objexception” exception object dim objexception as new exception(“cannot create directory”) ‘ creates an erroreventargs with the exception. dim objerroreventargs as new erroreventargs(objexception) if directory.exists(strmypath) = true then directory.delete(strmypath) else directory.createdirectory (strmypath) end if ‘ retrieves exception from erroreventargs dim objreturnedexception as exception= objerroreventargs.getexception() if objreturnedexception.message <> “” then messagebox.show(objreturnedexception.message end if end sub

    9. the class file provides to manage the file manipulation operations such as creating, opening, deleting, copying and moving of files using filestream objects. file class methods are static.

    some of the public methods used with file class is as follows:

      appendtext
      copy
      create
      createtext
      delete
      exists
      getattributes
      getcreationtime
      getcreationtimeutc
      getlastaccesstime
      getlastaccesstimeutc
      getlastwritetime
      move
      open
      openread
      opentext
      openwrite
      setattributes
      setcreationtime
      setcreationtimeutc
      setlastaccesstime
      setlastaccesstimeutc
      setlastwritetime
      setlastwritetimeutc

    usage

    ‘ initializing file class aids in the creation of filestream objects try dim strmypath as string=”c:\benoy\temp.txt” dim strmycopypath as string = _ “c:\benoy\temp1.txt” dim objfs as filestream = _ file.create(strmypath) ‘ closing the file objfs.close ‘ copy the file file.copy(strmypath,strmycopypath ‘ delete the file file.delete(strmypath) catch messagebox.show(“error in file operation”) end try

    10. the class fileinfo provides the same file management operation as in file class such as creating, opening, deleting, copying and moving of files using filestream objects, except if you are not reusing the object several times because the security check is not always necessary.

    11. the class fileloadexception throws exception error if any error occurs during file load.

    some of the public methods used with this class are shown below:

      filename – the associated filename that causes exception.
      fusionlog – get the log file.
      helplink - help link association during exception error.
      innerexception – an inner exception that caused the current exception.
      message - exception error message.
      source – error source.
      stacktrace – the associated string representation of the frames on the call stack.
      targetsite – the associated method name that causes exception.

    12. the filenotfoundexception class throws the exception error when the associated file is not found in the specified path or invalid disk access. the public methods are same as fileloadexception class.

    13. the filestream class supports to read, write, open and close the file management operations as well as it supports to manage the operating system file operations such as pipes, standard input and output.

    14. the filesystemeventargs class provides data for the directory events as shown below.

    changed event handler fires the properties or security details such as size, system attributes, last write time, last access time, whenever a file is changed or updated.

    created event handler fires whenever a directory or file created in a specified path.

    deleted event handler fires whenever a file or directory is deleted from the specified path.


    15. the filesysteminfo class supports the public methods which you can use for both files and directory in the specified path, which serving as the basis for two objects such as fileinfo and directoryinfo, which we understand from above.

    16. the filesystemwatcher class allows to notify or fires any changes occurs in the file system or directory such as a file or directory changed, deleted or created.

    the figure 1 shows the notifyfilter properties, that you can use to watch the notifications.




    figure 1 notifyfilter properties


    usage:

    ‘ create a new filesystemwatcher and its properties try dim objfilesystemwatcher as new _ filesystemwatcher() ‘ watch the notification for lastaccess and lastwrite, filename, crationtime and the renaming of files and directories objfilesystemwatcher.notifyfilter= (notifyfilters.lastwrite or notifyfilters.lastaccess or notifyfilters.filename or notifyfilters.directoryname or notifyfilters.filename or notifyfilters.creationtime) ‘ only watch doc files objfilesystemwatcher.filter = “*.doc” ‘ add event handlers addhandler objfilesystemwatcher.changed, addressof onchanged addhandler objfilesystemwatcher.created, addressof oncreated addhandler objfilesystemwatcher.deleted, addressof onchanged addhandler objfilesystemwatcher.renamed, addressof onchanged ‘ begin watching event for changed, deleted, renamed and created objfilesystemwatcher.enableraisingevents=true define the event handlers. private shared sub onchanged(byval source as object, byval e as filesystemeventargs) specify what is done when a file is changed, created, or deleted. messagebox.show("file: " & e.fullpath & " " & e.changetype) end sub private shared sub oncreated(byval source as object, byval e as renamedeventargs) specify what is done when a file is renamed. messagebox.show("file: {0} renamed to {1}", e.oldfullpath, e.fullpath) catch messagebox.show(“error in file operation”) end try


    17. the internalbufferoverflowexception class throws exception when the buffer overflows during execution of the programs. it supports some of the public methods as shown in the fileloadexception class except fusionlog and filename, which you saw above.

    18. the ioexception class throws exception when input/output error occurs. . it supports some of the public methods as shown in the fileloadexception class except fusionlog and filename, which you saw above.

    19. the memorystream class creates streams that have memory as an array instead of a disk or any storage media. it can reduce the need for temporary buffers and files in an application.

    usage: 

    try ‘ create a new instance of memorystream object dim objmemorystream as new memorystream(200) ‘ create an instance of unicodeencoding dim objunicodeencoding as new unicodeencoding() ‘ create array of string variables. dim strmytext as byte() = _ objunicodeencoding.getbytes("file management using whidbey/vb.net") objmemorystream.write(strmytext, 0 , strmytext.length) messagebox.show(“ _ "capacity = {0}, “ _ “length = {1}, “ _ “position = {2}", _ objmemorystream.capacity.tostring(), _ objmemorystream.length.tostring(), _ objmemorystream.position.tostring()) catch messagebox.show(“error in file operation”) end try

    20. other class members for this namespaces are

      path
      pathtoolongexception
      renamedeventargs
      stream
      streamreader class
      streamwriter
      stringreader
      stringwriter
      textreader
      textreader


    system.security.permissions

    the enumeration to get or set file permissions in vb.net is called fileiopermissionaccess enumerations, which used with the fileiopermission class. the system namespace used for this enumeration is system.security.permissions namespace, which implements iunrestrictedpermission interface, which defines classes that control access to operations and resources based on permission policy.

    classes that supports system.security.permissions

    the following classes will support system.security.permissions in vb.net.

      codeaccesssecurityattribute
      environmentpermission
      environmentpermissionattribute
      filedialogpermission
      filedialogpermissionattribute
      fileiopermission
      filedialogpermission
      fileiopermissionattribute
      isolatedstoragefilepermission
      isolatedstoragefilepermissionattribute
      isolatedstoragepermission
      isolatedstoragepermissionattribute
      permissionsetattribute
      principalpermission
      principalpermissionattribute
      publishedidentitypermission
      publishedidentitypermissionattribute
      reflectionpermission
      reflectionpermissionattribute
      registrypermission
      registrypermissionattribute
      resourcepermission
      resourcepermissionentry
      securityattribute
      securitypermission
      securitypermissionattribute
      siteidentitypermission
      siteidentitypermissionattribute
      strongnameidentitypermission
      strongnameidentitypermissionattribute
      strongnamepublickeyblob
      uipermission
      uipermissionattribute
      urlidentitypermission
      urlidentitypermissionattribute
      zoneidentitypermission
      zoneidentitypermissionattribute


    mode, share and access of files

    filemode

    filemode parameter control has the following members:

    o append – append to an existing file or create a new file if the file not exists
    o create - create a new file
    o open – open the file
    o opencreate - open a file if exists or it will create a file
    o truncate – remove all the contents so that its size is zero bytes


    example:

    dim objstreamreader as new streamreader(“c:\benoy\mytext.txt”) messagebox.show(objstreamreader.readtoend() objstreamreader.close()

    fileshare

    fileshare enumeration allows you to control the share permission to access the filestreams.

    some of the members included in the fileshare enumerations are read, readwrite, write, none, inheritable etc.

    read command allows you to opening the file for reading.
    readwrite command allows you to opening the file for reading and writing.
    write command allows you to opening the file for writing.
     

    dim objfilestream as new filestream(“c:\benoy\mytext.txt”, _ filemode. openorcreateopen, fileaccess.readwrite, fileshare.readwrite)

    fileaccess

    fileaccess enumeration allows you to control the permission to access the filestreams.

    some of the members included in the fileaccess enumerations are read, readwrite, write etc.


    conclusion

    in this article, we found how to manage windows file operations using different file level operation commands. it allows you to set file permissions, modes, access and sharing of files using vb.net environment.

    ·  file system management and its uses
    ·  file permissions
    ·  file modes, file access, file shares


    benoyrajb
    benoyraj baskaran is a senior consultant with ga department of transportation, atlanta. previously, j was a consultant with autozone inc. and has been developing solutions on the microsoft platform for over 10 years.


     

     
    热门推荐笔记本: 富士通笔记本
    相关文章:
    webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
    ©2007 popbb.net All Rights Reserved