删除指定目录里的所有文件

VB.NET
        <System.ComponentModel.Description("删除目录内所有文件")> _
        
Public Shared Function DelFiles(ByVal sPhysicalDirectory As System.StringOptional ByVal isPhysicalDirectory As Boolean = TrueAs Boolean
            
Dim oDirectoryInfo As System.IO.DirectoryInfo
            
Dim oFileInfo As System.IO.FileInfo

            
If isPhysicalDirectory = False Then
                sPhysicalDirectory 
= System.Web.HttpContext.Current.Server.MapPath(sPhysicalDirectory)
            
End If

            
Try
                
If System.IO.Directory.Exists(sPhysicalDirectory) = False Then Exit Function

                oDirectoryInfo 
= New System.IO.DirectoryInfo(sPhysicalDirectory)
                
For Each oFileInfo In oDirectoryInfo.GetFiles
                    System.IO.File.Delete(oFileInfo.FullName)
                
Next
                
Return True
            
Catch ex As System.Exception
                Common.Exception.Append(ex)
                
Return False
            
End Try

        
End Function

C#
[System.ComponentModel.Description("删除目录内所有文件")] 
public static bool DelFiles(System.String sPhysicalDirectory) 

 DelFiles(sPhysicalDirectory, 
true
}
 

[System.ComponentModel.Description(
"删除目录内所有文件")] 
public static bool DelFiles(System.String sPhysicalDirectory, bool isPhysicalDirectory) 

 System.IO.DirectoryInfo oDirectoryInfo; 
 System.IO.FileInfo oFileInfo; 
 
if (isPhysicalDirectory == false
   sPhysicalDirectory 
= System.Web.HttpContext.Current.Server.MapPath(sPhysicalDirectory); 
 }
 
 
try 
   
if (System.IO.Directory.Exists(sPhysicalDirectory) == false
     
goto exitMethodDeclaration0; 
   }
 
   oDirectoryInfo 
= new System.IO.DirectoryInfo(sPhysicalDirectory); 
   
foreach (int oFileInfo in oDirectoryInfo.GetFiles) 
     System.IO.File.Delete(oFileInfo.FullName); 
   }
 
   
return true
 }
 catch (System.Exception ex) 
   Common.Exception.Append(ex); 
   
return false
 }
 
 exitMethodDeclaration0: ; 
}

posted on 2004-09-24 10:05  小牛哥  阅读(2484)  评论(4编辑  收藏  举报

导航