none
求助--檔案無法刪除 RRS feed

  • 問題

  • 我是用VB2005,遇到唯讀檔的時候就跑列外處理,檔案無法刪除‧

    VB6用API刪除都OK,在2005遇到一堆的問題了,求助--QQQ!!!

    2007年6月10日 上午 12:03

解答

  • 我已經改用api來刪了,等我2005學在多一點再來寫,謝謝。

    以下為api

     'wFunc 可用的常數如下:
        Public Const FO_MOVE As Short = &H1S
        Public Const FO_COPY As Short = &H2S
        Public Const FO_DELETE As Short = &H3S
        Public Const FO_RENAME As Short = &H4S

        'fFlags 可用的常數如下:
        Public Const FOF_ALLOWUNDO As Short = &H40S
        Public Const FOF_FILESONLY As Short = &H80S
        Public Const FOF_MULTIDESTFILES As Short = &H1S
        Public Const FOF_NOCONFIRMATION As Short = &H10S '不出現提示 "是否覆蓋檔案" 的問話框.
        Public Const FOF_NOCONFIRMMKDIR As Short = &H200S '若需建立新目錄時,不出現確認問話框.
        Public Const FOF_NO_CONNECTED_ELEMENTS As Short = &H1000S
        Public Const FOF_NOCOPYSECURITYATTRIBS As Short = &H800S
        Public Const FOF_NOERRORUI As Short = &H400S
        Public Const FOF_RENAMEONCOLLISION As Short = &H8S
        Public Const FOF_SILENT As Short = &H4S
        Public Const FOF_SIMPLEPROGRESS As Short = &H100S
        Public Const FOF_WANTMAPPINGHANDLE = &H20S


        '********************** 刪除資料夾模組 **************************
        Public Declare Auto Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperation" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer

        '********************** 刪除資料夾模組 **************************
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, pack:=1)> _
        Public Structure SHFILEOPSTRUCT
            Public hWnd As Integer
            Public wFunc As Integer
            Public pFrom As String
            Public pTo As String
            Public fFlags As Short
            Public fAnyOperationsAborted As Boolean
            Public hNameMappings As Integer
            Public lpszProgressTitle As Integer
        End Structure

     

     

     '刪除API
        Private Sub DeleteFolder(ByVal DelFolder As String)
            Dim ShellCall As New SHFILEOPSTRUCT
            With ShellCall
                .hWnd = Handle.ToInt32
                .wFunc = FO_DELETE
                .pFrom = DelFolder
                .fFlags = FOF_NOCONFIRMATION
            End With
            SHFileOperation(ShellCall)

          '若要調整,只要更改 fFlags 之值即可,如下:
            '   .fFlags = FOF_NOCONFIRMATION          '刪除檔案,不出現確認對話方塊,檔案也不移到資源回收筒。
            '   .fFlags = FOF_SILENT                  '刪除檔案,出現確認對話方塊,檔案不移到資源回收筒。
            '   .fFlags = FOF_ALLOWUNDO               '刪除檔案,出現確認對話方塊,檔案移到資源回收筒。
        End Sub

    2007年6月12日 下午 08:53

所有回覆

  • 唯讀檔用 API 也刪不掉吧,API 的文件中有寫到:

     

    If the file is a read-only file, the function fails with ERROR_ACCESS_DENIED.

     

    只是 API 是不會丟出例外的,必須要使用 GetLastError() API 才可以抓的到這個錯誤 .

     

    而在 Visual Basic 中,File.Delete(), FileInfo.Delete() 等,會自動吐出例外 .

    2007年6月10日 上午 01:04
    版主
  • 那請教有辦法嗎,我一堆程式碼都刪不掉唯讀檔,ㄟ死!

    謝謝大大門的指教。

    2007年6月10日 上午 01:56
  • :::

            If (File.GetAttributes("c:\zabcabc.txt") And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                File.SetAttributes("c:\zabcabc.txt", FileAttributes.Archive)
                File.Delete("c:\zabcabc.txt")
            End If

    2007年6月10日 上午 03:58
  • 使用 File.Delete() 方法不能刪除唯讀檔案,因此,如果欲刪除的檔案屬性為唯讀,那麼其 Attributes 屬性中會包含有 readonly 的字串,只需先把檔案屬性重新設定為 Normal,然後再刪除即可:
    程式碼片段
    Dim myFile As String = "C:\Alex.txt"
    Try
       Dim fi As IO.FileInfo = New IO.FileInfo(myFile)
       If fi.Attributes.ToString().IndexOf("readonly") <> -1 Then
           fi.Attributes = IO.FileAttributes.Normal
           IO.File.Delete(myFile)
       End If
    Catch ex As Exception
       System.Diagnostics.Debug.WriteLine(ex.ToString)
       Console.WriteLine(ex.Message)
    End Try
    2007年6月10日 上午 06:19
  • 我已經改用api來刪了,等我2005學在多一點再來寫,謝謝。

    以下為api

     'wFunc 可用的常數如下:
        Public Const FO_MOVE As Short = &H1S
        Public Const FO_COPY As Short = &H2S
        Public Const FO_DELETE As Short = &H3S
        Public Const FO_RENAME As Short = &H4S

        'fFlags 可用的常數如下:
        Public Const FOF_ALLOWUNDO As Short = &H40S
        Public Const FOF_FILESONLY As Short = &H80S
        Public Const FOF_MULTIDESTFILES As Short = &H1S
        Public Const FOF_NOCONFIRMATION As Short = &H10S '不出現提示 "是否覆蓋檔案" 的問話框.
        Public Const FOF_NOCONFIRMMKDIR As Short = &H200S '若需建立新目錄時,不出現確認問話框.
        Public Const FOF_NO_CONNECTED_ELEMENTS As Short = &H1000S
        Public Const FOF_NOCOPYSECURITYATTRIBS As Short = &H800S
        Public Const FOF_NOERRORUI As Short = &H400S
        Public Const FOF_RENAMEONCOLLISION As Short = &H8S
        Public Const FOF_SILENT As Short = &H4S
        Public Const FOF_SIMPLEPROGRESS As Short = &H100S
        Public Const FOF_WANTMAPPINGHANDLE = &H20S


        '********************** 刪除資料夾模組 **************************
        Public Declare Auto Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperation" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer

        '********************** 刪除資料夾模組 **************************
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, pack:=1)> _
        Public Structure SHFILEOPSTRUCT
            Public hWnd As Integer
            Public wFunc As Integer
            Public pFrom As String
            Public pTo As String
            Public fFlags As Short
            Public fAnyOperationsAborted As Boolean
            Public hNameMappings As Integer
            Public lpszProgressTitle As Integer
        End Structure

     

     

     '刪除API
        Private Sub DeleteFolder(ByVal DelFolder As String)
            Dim ShellCall As New SHFILEOPSTRUCT
            With ShellCall
                .hWnd = Handle.ToInt32
                .wFunc = FO_DELETE
                .pFrom = DelFolder
                .fFlags = FOF_NOCONFIRMATION
            End With
            SHFileOperation(ShellCall)

          '若要調整,只要更改 fFlags 之值即可,如下:
            '   .fFlags = FOF_NOCONFIRMATION          '刪除檔案,不出現確認對話方塊,檔案也不移到資源回收筒。
            '   .fFlags = FOF_SILENT                  '刪除檔案,出現確認對話方塊,檔案不移到資源回收筒。
            '   .fFlags = FOF_ALLOWUNDO               '刪除檔案,出現確認對話方塊,檔案移到資源回收筒。
        End Sub

    2007年6月12日 下午 08:53