【问题标题】:Deleting temp folder files with vb.net使用 vb.net 删除临时文件夹文件
【发布时间】:2012-10-02 07:58:35
【问题描述】:

我正在尝试以编程方式清除我的 temps 文件夹,但它无法删除正在使用的文件,只要删除所有未使用的文件就可以了。但是,我的代码基本上要求我们删除全部或不删除,而不仅仅是那些未使用的。

下面是代码,谁能告诉我如何解决这个问题?

   'Deletes files in temporary directory...
    Dim MYDAYS_2 As String
    Dim MYTEMPFOLDER As String = System.IO.Path.GetTempPath

    'this reads the regestry key otherwise gives a default value in its place (365)...
    MYDAYS_2 = Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\AA", "HELLO", "365")

    'Deletes all files older then the day specifyed in the variable MDAYS...
    For Each file As IO.FileInfo In New IO.DirectoryInfo(MYTEMPFOLDER).GetFiles("*.*")
        If (Now - file.CreationTime).Days > MYDAYS_2 Then file.Delete()
    Next

【问题讨论】:

    标签: vb.net file delete-file temp


    【解决方案1】:

    您可以使用简单的 try/catch:

    For Each file As IO.FileInfo In New IO.DirectoryInfo(MYTEMPFOLDER).GetFiles("*.*")
        If (Now - file.CreationTime).Days > MYDAYS_2 Then 
            Try
                file.Delete()
            Catch
                ' log exception or ignore '
            End Try
        End If
    Next
    

    【讨论】:

    • 这个工作会持续 4 分钟,直到我能接受你的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多