删除指定目录里的所有文件
VB.NET
C#
<System.ComponentModel.Description("删除目录内所有文件")> _
Public Shared Function DelFiles(ByVal sPhysicalDirectory As System.String, Optional ByVal isPhysicalDirectory As Boolean = True) As 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
Public Shared Function DelFiles(ByVal sPhysicalDirectory As System.String, Optional ByVal isPhysicalDirectory As Boolean = True) As 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: ;
}
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: ;
}