文件操作的CLASS(VB)

 

< %
class cls_fso 
    
public  objfso 
    
private   sub  class_initialize() 
        
set  objfso  =  server.createobject( " scripting.filesystemobject "
    
end sub  
    
private   sub  class_terminate() 
        
set  objfso  =   nothing  
    
end sub  
    
    
' 文件是否存在
     public   function  reportfilestatus(filename) 
        
dim  msg 
        msg 
=   - 1  
        
if  (objfso.fileexists(filename))  then  
            msg 
=   1  
        
else  
            msg 
=   - 1  
        
end   if  
        reportfilestatus 
=  msg 
    
end function  

    
' =======文件操作======== 
     ' 取文件大小
     public   function  getfilesize(filename) 
        
dim  f 
        
if  reportfilestatus(filename)  =   1   then  
            
set  f  =  objfso.getfile(filename) 
            getfilesize 
=  f.size 
        
else  
            getfilesize 
=   - 1  
        
end   if  
    
end function  

    
' 刪除文件
     public   function  deleteafile(filespec) 
        
if  reportfilestatus(filespec)  =   1   then  
            objfso.deletefile(filespec) 
            deleteafile 
=   1  
        
else  
            deleteafile 
=   - 1  
        
end   if  
    
end function  

    
' 顯示文件列表
     public   function  showfilelist(folderspec) 
        
dim  f, f1, fc, s 
        
if  reportfolderstatus(folderspec)  =   1   then  
            
set  f  =  objfso.getfolder(folderspec) 
            
set  fc  =  f.files 
            
for   each  f1 in fc 
                s 
=  s  &  f1.name 
                s 
=  s  &   " | "  
            
next  
            showfilelist 
=  s 
        
else  
            showfilelist 
=   - 1  
        
end   if  
    
end function  

    
' 復制文件
     public   function  copyafile(sourcefile, destinationfile) 
        
dim  myfile 
        
if  reportfilestatus(sourcefile)  =   1   then  
            
set  myfile  =  objfso.getfile(sourcefile) 
            myfile.copy (destinationfile) 
            copyafile 
=   1  
        
else  
            copyafile 
=   - 1  
        
end   if  
    
end function  

    
' 移動文件
     public   function  moveafile(sourcefile,destinationfile) 
        
if  reportfilestatus(sourcefile)  =   1   and  reportfilestatus(destinationfileorpath)  =   - 1   then  
            objfso.movefile sourcefile,destinationfileorpath 
            moveafile 
=   1  
        
else  
            moveafile 
=   - 1  
        
end   if  
    
end function  


    
' 文件創建日期
     public   function  showdatecreated(filespec) 
        
dim  f 
        
if  reportfilestatus(filespec)  =   1   then  
            
set  f  =  objfso.getfile(filespec) 
            showdatecreated 
=  f.datecreated 
        
else  
            showdatecreated 
=   - 1  
        
end   if  
    
end function  

    
' 文件屬性
     public   function  getattributes(filename) 
        
dim  f 
        
dim  strfileattributes 
        
if  reportfilestatus(filename)  =   1   then  
            
set  f  =  objfso.getfile(filename) 
            
select   case  f.attributes 
                
case   0  strfileattributes  =   " 普通文件,沒有設置任何屬性. "  
                
case   1  strfileattributes  =   " 只讀文件,可讀寫.  "  
                
case   2  strfileattributes  =   " 隱藏文件,可讀寫. "  
                
case   4  strfileattributes  =   " 系統文件,可讀寫. "  
                
case   16  strfileattributes  =   " 文件夾或目錄,只讀. "  
                
case   32  strfileattributes  =   " 上次備份後已更改的文件,可讀寫. "  
                
case   1024  strfileattributes  =   " 鏈接或是快捷方式,只讀. "  
                
case   2048  strfileattributes  =   " 壓縮文件,只讀. "  
            
end   select  
            getattributes 
=  strfileattributes 
        
else  
            getattributes 
=   - 1  
        
end   if  
    
end function  

    
' 最後一次訪問,最後一次修改時間
     public   function  showfileaccessinfo(filename,infotype) 
        
' //功能:顯示文件創建信息 
         ' //形參:文件名,信息類別.
         ' // 1 -----創建時間
         ' // 2 -----上次訪問時間
         ' // 3 -----上次修改時間 
         ' // 4 -----文件路徑
         ' // 5 -----文件名稱 
         ' // 6 -----文件類型 
         ' // 7 -----文件大小 
         ' // 8 -----父目錄 
         ' // 9 -----根目錄 
         dim  f, s 
        
if  reportfilestatus(filename)  =   1   then  
            
set  f  =  objfso.getfile(filename) 
            
select   case  infotype 
                
case   1  s  =  f.datecreated 
                
case   2  s  =  f.datelastaccessed 
                
case   3  s  =  f.datelastmodified 
                
case   4  s  =  f.path 
                
case   5  s  =  f.name 
                
case   6  s  =  f.type 
                
case   7  s  =  f.size 
                
case   8  s  =  f.parentfolder 
                
case   9  s  =  f.rootfolder 
            
end   select  
            showfileaccessinfo 
=  s 
        
else  
            showfileaccessinfo 
=   - 1  
        
end   if  
    
end function  

    
' 寫文件
     public   function  writetxtfile(filename,textstr,writeorappendtype) 
        
const  forreading  =   1 , forwriting  =   2  , forappending  =   8  
        
dim  f, m 
        
select   case  writeorappendtype 
            
case   1 ' 文件進行寫操作
                 set  f  =  objfso.opentextfile(filename, forwriting,  true
                f.write textstr 
                f.close 
                
if  reportfilestatus(filename)  =   1   then  
                    writetxtfile 
=   1  
                
else  
                    writetxtfile 
=   - 1  
                
end   if  
            
case   2 ' 文件尾進行寫操作 
                 if  reportfilestatus(filename)  =   1   then  
                    
set  f  =  objfso.opentextfile(filename, forappending) 
                    f.write textstr 
                    f.close 
                    writetxtfile 
=   1  
                
else  
                    writetxtfile 
=   - 1  
                
end   if  
            
end   select  
    
end function  

    
' 讀文本文件
     public   function  readtxtfile(filename) 
        
const  forreading  =   1 , forwriting  =   2  
        
dim  f, m 
        
if  reportfilestatus(filename)  =   1   then  
            
set  f  =  objfso.opentextfile(filename, forreading) 
            m 
=  f.readline 
            readtxtfile 
=  m 
            f.close 
        
else  
            readtxtfile 
=   - 1  
        
end   if  
    
end function  

    
    
' =======目錄操作======== 
     ' 目錄是否存在
     public   function  reportfolderstatus(fldr) 
        
dim  msg 
        msg 
=   - 1  
        
if  (objfso.folderexists(fldr))  then  
            msg 
=   1  
        
else  
            msg 
=   - 1  
        
end   if  
        reportfolderstatus 
=  msg 
    
end function  
    
' 取目錄大小
     public   function  getfoldersize(foldername) 
        
dim  f 
        
if  reportfolderstatus(foldername)  =   1   then  
            
set  f  =  objfso.getfolder(foldername) 
            getfoldersize 
=  f.size 
        
else  
            getfoldersize 
=   - 1  
        
end   if  
    
end function  

    
' 創建新的目錄
     public   function  createfolderdemo(foldername) 
        
dim  f 
        
if  reportfolderstatus(folderspec)  =   1   then  
            createfolderdemo 
=   - 1  
        
else  
            
set  f  =  objfso.createfolder(foldername) 
            createfolderdemo 
=   1  
        
end   if  
    
end function  

    
' 刪除目錄
     public   function  deleteafolder(folderspec) 
        response.write folderspec 
        
if  reportfolderstatus(folderspec)  =   1   then  
            objfso.deletefolder (folderspec) 
            deleteafolder 
=   1  
        
else  
            deleteafolder 
=   - 1  
        
end   if  
    
end function  

    
' 顯示目錄列表
     public   function  showfolderlist(folderspec) 
        
dim  f, f1, fc, s 
        
if  reportfolderstatus(folderspec)  =   1   then  
            
set  f  =  objfso.getfolder(folderspec) 
            
set  fc  =  f.subfolders 
            
for   each  f1 in fc 
                s 
=  s  &  f1.name 
                s 
=  s  &   " | "  
            
next  
            showfolderlist 
=  s 
        
else  
            showfolderlist 
=   - 1  
        
end   if  
    
end function  

    
' 目錄復制
     public   function  copyafolder(sourcefolder,destinationfolder) 
        objfso.copyfolder sourcefolder,destinationfolder 
        copyafolder 
=   1  
        copyafolder 
=   - 1  
    
end function  


    
' 目錄進行移動
     public   function  moveafolder(sourcepath,destinationpath) 
        
if  reportfolderstatus(sourcepath) = 1   and  reportfolderstatus(destinationpath) = 0   then  
            objfso.movefolder sourcepath, destinationpath 
            moveafolder 
=   1  
        
else  
            moveafolder 
=   - 1  
        
end   if  
    
end function  


    
' 目錄創建信息
     public   function  showfolderaccessinfo(foldername,infotype) 
        
' //功能:顯示目妹創建時信息 
         ' //形參:目錄名,信息類別 
         ' // 1 -----創建時間 
         ' // 2 -----上次訪問時間 
         ' // 3 -----上次修改時間 
         ' // 4 -----目錄路徑
         ' // 5 -----目錄名稱 
         ' // 6 -----目錄類型 
         ' // 7 -----目錄大小 
         ' // 8 -----父目錄
         ' // 9 -----根目錄 
         dim  f, s 
        
if  reportfolderstatus(foldername)  =   1   then  
            
set  f  =  objfso.getfolder(foldername) 
            
select   case  infotype 
                
case   1  s  =  f.datecreated 
                
case   2  s  =  f.datelastaccessed 
                
case   3  s  =  f.datelastmodified 
                
case   4  s  =  f.path 
                
case   5  s  =  f.name 
                
case   6  s  =  f.type 
                
case   7  s  =  f.size 
                
case   8  s  =  f.parentfolder 
                
case   9  s  =  f.rootfolder 
            
end   select  
            showfolderaccessinfo 
=  s 
        
else  
            showfolderaccessinfo 
=   - 1  
        
end   if  
    
end function  

    
' 遍歷目錄
     public   function  displayleveldepth(pathspec) 
        
dim  f, n ,path 
        
set  f  =  objfso.getfolder(pathspec) 
        
if  f.isrootfolder  then  
            displayleveldepth 
= " 指寫的文件夾是根文件夾. " & rootfolder 
        
else  
            
do  until f.isrootfolder 
                path 
=  path  &  f.name  & " <br> "  
                
set  f  =  f.parentfolder 
                n 
=  n  +   1  
            
loop  
            displayleveldepth 
= " 指寫的文件夾是嵌套級為: "   &  n  &   "  的文件夾.<br> "   &  path 
        
end   if  
    
end function  

    
' ========磁盤操作======== 
     ' 驅動器是否存在
     public   function  reportdrivestatus(drv) 
        
dim  msg 
        msg 
=   - 1  
        
if  objfso.driveexists(drv)  then  
            msg 
=   1  
        
else  
            msg 
=   - 1  
        
end   if  
        reportdrivestatus 
=  msg 
    
end function  

    
' 可用的返回類型包括fat,ntfs,cdfs.
     public   function  showfilesystemtype(drvspec) 
        
dim  d 
        
if  reportdrivestatus(drvspec)  =   1   then  
            
set  d  =  objfso.getdrive(drvspec) 
            showfilesystemtype 
=  d.filesystem 
        
else  
            showfilesystemtype 
=   - 1  
        
end   if  
    
end function  
end  class 
%
>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是一个 VB.NET 操作 XML 文件的示例: 1. 创建 XML 文件: ``` Dim xmlDoc As New XmlDocument() Dim xmlDeclaration As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing) xmlDoc.AppendChild(xmlDeclaration) Dim rootNode As XmlElement = xmlDoc.CreateElement("Root") xmlDoc.AppendChild(rootNode) xmlDoc.Save("test.xml") ``` 2. 读取 XML 文件: ``` Dim xmlDoc As New XmlDocument() xmlDoc.Load("test.xml") Dim rootNode As XmlElement = xmlDoc.DocumentElement For Each childNode As XmlNode In rootNode.ChildNodes Console.WriteLine(childNode.Name & ": " & childNode.InnerText) Next ``` 3. 修改 XML 文件: ``` Dim xmlDoc As New XmlDocument() xmlDoc.Load("test.xml") Dim rootNode As XmlElement = xmlDoc.DocumentElement For Each childNode As XmlNode In rootNode.ChildNodes If childNode.Name = "NodeName" Then childNode.InnerText = "New Value" End If Next xmlDoc.Save("test.xml") ``` 希望这些代码对你有所帮助。 ### 回答2: VB.net中操作XML文件常用的类是XmlDocument类和XmlNode类。下面是一个使用VB.net操作XML文件的示例。 ```vb.net Imports System.Xml Public Class XMLExample Public Sub ReadXMLFile(filePath As String) ' 创建一个XmlDocument对象 Dim xmlDoc As New XmlDocument() ' 加载XML文件 xmlDoc.Load(filePath) ' 获取根节点 Dim rootNode As XmlNode = xmlDoc.DocumentElement ' 遍历根节点下的所有子节点 For Each childNode As XmlNode In rootNode.ChildNodes ' 输出子节点的名称和内容 Console.WriteLine("节点名称: " & childNode.Name) Console.WriteLine("节点内容: " & childNode.InnerText) Next End Sub Public Sub WriteXMLFile(filePath As String) ' 创建一个XmlDocument对象 Dim xmlDoc As New XmlDocument() ' 创建根节点 Dim rootNode As XmlNode = xmlDoc.CreateElement("Books") ' 将根节点添加到XmlDocument对象中 xmlDoc.AppendChild(rootNode) ' 创建子节点 Dim bookNode As XmlNode = xmlDoc.CreateElement("Book") ' 创建子节点的属性 Dim attrib As XmlAttribute = xmlDoc.CreateAttribute("ISBN") attrib.Value = "978-7-121-32712-3" ' 将属性添加到子节点中 bookNode.Attributes.Append(attrib) ' 将子节点添加到根节点中 rootNode.AppendChild(bookNode) ' 创建子节点的子节点 Dim titleNode As XmlNode = xmlDoc.CreateElement("Title") titleNode.InnerText = "VB.net XML文件操作实例" ' 将子节点的子节点添加到子节点中 bookNode.AppendChild(titleNode) ' 保存XML文件 xmlDoc.Save(filePath) End Sub End Class ' 使用示例 Private Sub Main() Dim example As New XMLExample() Dim filePath As String = "example.xml" ' 写入XML文件 example.WriteXMLFile(filePath) ' 读取XML文件 example.ReadXMLFile(filePath) End Sub ``` 上述示例中,提供了读取XML文件和写入XML文件的两个方法。创建了一个XmlDocument对象来加载和操作XML文件。通过XmlDocument对象的方法和属性,可以方便地读取和修改XML文件的内容。读取XML文件时,通过遍历节点的方式获取节点的名称和内容。写入XML文件时,通过创建节点和属性的方式构建XML节点树,并将节点添加到XmlDocument对象中。最后使用XmlDocument对象的Save方法将XML文件保存到磁盘中。 ### 回答3: 在VB.net中,我们可以使用System.Xml命名空间下的类来进行XML文件操作。下面是一个XML文件操作的实例: 首先,我们需要在程序中引入System.Xml的命名空间,以便使用相应的类。可以在代码文件的顶部添加以下代码: ```vb Imports System.Xml ``` 接下来,我们需要创建一个XmlDocument对象来加载XML文件。假设我们有一个名为"example.xml"的XML文件,它的内容如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <products> <product> <id>1</id> <name>Product1</name> <price>10.0</price> </product> <product> <id>2</id> <name>Product2</name> <price>20.0</price> </product> </products> ``` 我们可以使用以下代码来加载XML文件: ```vb Dim xmlDoc As New XmlDocument() xmlDoc.Load("example.xml") ``` 接下来,我们可以使用SelectNodes或SelectSingleNode方法来选择XML节点。例如,要选择所有的product节点,可以使用以下代码: ```vb Dim productNodes As XmlNodeList = xmlDoc.SelectNodes("/products/product") ``` 如果要选择某个具体的节点,可以使用SelectSingleNode方法,并传入XPath表达式。例如,要选择第一个product节点的name子节点,可以使用以下代码: ```vb Dim nameNode As XmlNode = xmlDoc.SelectSingleNode("/products/product[1]/name") ``` 要访问节点的内容,可以使用InnerText属性或Value属性。例如,要获取第一个product节点的name子节点的文本内容,可以使用以下代码: ```vb Dim name As String = nameNode.InnerText ``` 如果要修改节点的内容,可以直接修改节点的InnerText属性。例如,要将第一个product节点的name子节点的文本内容修改为"New Product",可以使用以下代码: ```vb nameNode.InnerText = "New Product" ``` 最后,我们需要保存修改后的XML文件。可以使用Save方法来保存XML文件。例如,要保存修改后的XML文件为"example_modified.xml",可以使用以下代码: ```vb xmlDoc.Save("example_modified.xml") ``` 以上就是一个简单的VB.net中操作XML文件的示例。通过使用System.Xml命名空间下的类,我们可以加载、选择、修改和保存XML文件中的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值