代码改变世界

我最近写的通用安全文件夹删除函数,跟大家分享

2004-05-20 11:54  灵感之源  阅读(1351)  评论(2编辑  收藏  举报

鉴于删除文件夹操作比较特殊,需要安全一点,所以封装成一通用安全文件夹删除函数,跟大家分享:

    Public Shared Function DeleteFolder(ByVal strDirectory As String) As Boolean
        '名称:DeleteFolder
        '功能:安全删除文件夹
        '作者:chs
        '创建:2004/2/5
        '修改:2004/2/5
        '参数::
        '      :
        '返回:成功,True,反之,False
        '说明:

        Dim o_blnRet As Boolean = False

        Try
            Dim o_objDir As New DirectoryInfo(strDirectory)
            Dim o_objFileInfo() As FileInfo = o_objDir.GetFiles()
            Dim o_intItems As Integer

            For o_intItems = 0 To o_objFileInfo.Length - 1
                With o_objFileInfo(o_intItems)
                    '把文件的属性该成普通,让其能正常删除
                    .Attributes = FileAttributes.Normal

                    '删除文件
                    .Delete()
                End With
            Next

            o_objDir.Delete()

            o_objFileInfo = Nothing
            o_objDir = Nothing

            o_blnRet = True
        Catch ex As Exception
            o_blnRet = False
        End Try

        Return o_blnRet
    End Function