locked
FILE_FLAG_DELETE_ON_CLOSE RRS feed

  • Question

  • I am trying to set the FILE_FLAG_DELETE_ON_CLOSE attribute on an existing file but the only references I can find are for creating a new file.

    Is this there a way to set this attribute to an existing file or is there another way to delete a file when the last handle on the file is closed?

    Monday, June 22, 2020 12:16 PM

Answers

  • Hi Young's,

    Thanks for your feedback.

    >>Can you give me some more details on how to check the all process handler and how to close the process

    Check the suggestion in the following reference:

    How do I delete a file which is locked by another process(C#)

    You need to convert the C# code to VB.NET, and if you need further assistance, please let me know.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Young's Friday, June 26, 2020 8:56 AM
    Thursday, June 25, 2020 5:55 AM

All replies

  • This deletes the existing file E:\test.txt on my Windows 10 OS when I close it =>

    HANDLE hFile = CreateFile(TEXT("E:\\test.txt"),	GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
    if (hFile != INVALID_HANDLE_VALUE)
    {
    	// ...
    	CloseHandle(hFile);
    }
    


    • Edited by Castorix31 Monday, June 22, 2020 12:41 PM
    Monday, June 22, 2020 12:39 PM
  • Hi,

    I am importing a spreadsheet into SQL FileStream and I want to delete the spreadsheet from the location it was uploaded from. I tried using setattributes to set FILE_FLAG_DELETE_ON_CLOSE but it says it is not a member of file attributes. Could you let me know how to do this in VB syntax please?

    This is my code

     Protected Sub Upload_Click(sender As Object, e As EventArgs) Handles Upload.Click
            Dim Cmd As SqlCommand
            Dim ConStr As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("CapexConnectionString")
            Dim Stage As Byte = 0
            Try
                If FileUpload1.HasFile Then
                    Dim filename As String = FileUpload1.FileName
                    Dim path As String = ConfigurationManager.AppSettings("EmailFolder") + filename
                    Dim uFile As String = ConfigurationManager.AppSettings("UploadFolder") + filename
                    If IO.File.Exists(uFile) Then
                        IO.File.Copy(uFile, path, True)
                        Threading.Thread.Sleep(1000)
                        Using Con As New SqlConnection(ConStr.ConnectionString)
                            Con.Open()
                            Cmd = New SqlCommand("spUploadFile", Con) With {
                            .CommandType = CommandType.StoredProcedure
                        }
                            Cmd.Parameters.AddWithValue("@Capex", Session("ProjectID"))
                            Cmd.Parameters.AddWithValue("@FileName", filename)
                            Cmd.Parameters.AddWithValue("@FilePath", path)
                            Cmd.ExecuteNonQuery()
                            Cmd.Dispose()
                        End Using
                        AttachmentList.DataBind()
                        'IO.File.SetAttributes(Path, IO.File.GetAttributes(Path) Or IO.FileAttributes.FILE_FLAG_DELETE_ON_CLOSE)
                    Else
                        mpe1.Show()
                    End If
                    Stage = 1
                End If
            Catch ex As Exception
                If Stage = 0 Then
                    Session("ErrorMsg") = ex.Message
                End If
            End Try
            If Stage = 0 Then
                Response.Redirect("~/ErrorPage.aspx")
            End If
        End Sub

    Monday, June 22, 2020 1:42 PM
  • In .NET, the flag is DeleteOnClose for FileStream

    But can't you just call File.Delete ?


    Monday, June 22, 2020 2:19 PM
  • File.Delete throws an error because the file is locked.

    It used to work in Edge. The file was deleted when the browser was closed but it doesn't work in the chromium version of Edge or in Chrome.

    I suspect Edge used to set the FILE_FLAG_DELETE_ON_CLOSE attribute, it certainly appeared to work as though it did.

    Monday, June 22, 2020 2:30 PM
  • Hi Young's,

    Thanks for your feedback.

    >>I tried using setattributes to set FILE_FLAG_DELETE_ON_CLOSE but it says it is not a member of file attributes.

    All the file attributes to set for the file in SetFileAttributesA function override FILE_ATTRIBUTE_NORMAL. No option for FILE_FLAG_DELETE_ON_CLOSE.

    >>File.Delete throws an error because the file is locked. 

    In order to delete locked files, you need check all process handler list to find which process locks this file and then close the process.

    Besides, you can also use the MoveFileEx API function to mark the file for deletion upon next reboot.

    For example:

        Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (
               ByVal lpExistingFileName As String,
               ByVal lpNewFileName As String,
               ByVal dwFlags As Long) As Long
    
        Private Const MOVEFILE_DELAY_UNTIL_REBOOT = &H4
    
       '...
        MoveFileEx(FileName, Nothing, MOVEFILE_DELAY_UNTIL_REBOOT)

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 24, 2020 6:02 AM
  • Hi,

    Thanks for the reply. Unfortunately the files are on a server which is rebooted infrequently and security requirements mean I need to delete the files straight away. The files are not being locked by anything that is external to the method where I am trying to delete them. The only thing it can be is the file copy command.

    Can you give me some more details on how to check the all process handler and how to close the process?

    Wednesday, June 24, 2020 7:34 AM
  • I have just tried running the code below to find the process that is locking the file I am trying to delete but it throws an error at the for loop for i > 0.

    Modules 'processes(1).Modules' threw an exception of type 'System.ComponentModel.Win32Exception' System.Diagnostics.ProcessModuleCollection {System.ComponentModel.Win32Exception}

    The function is called from my code above as follows.

    Dim a As ArrayList = getFileProcesses(uFile)

        Private Function getFileProcesses(ByVal strFile As String) As ArrayList
            myProcessArray.Clear()
            Dim processes As Process() = Process.GetProcesses
            Dim i As Integer
            For i = 0 To processes.GetUpperBound(0) - 1  'Throws an error here
                myProcess = processes(i)
                If Not myProcess.HasExited Then
                    Try
                        Dim modules As ProcessModuleCollection = myProcess.Modules
                        Dim j As Integer
                        For j = 0 To modules.Count - 1
                            If (modules.Item(j).FileName.ToLower.CompareTo(strFile.ToLower) = 0) Then
                                myProcessArray.Add(myProcess)
                                Exit For
                            End If
                        Next j
                    Catch exception As Exception
                        'MsgBox(("Error : " & exception.Message))
                    End Try
                End If
            Next i
            Return myProcessArray
        End Function





    • Edited by Young's Wednesday, June 24, 2020 8:46 AM
    Wednesday, June 24, 2020 8:34 AM
  • Hi Young's,

    Thanks for your feedback.

    >>Can you give me some more details on how to check the all process handler and how to close the process

    Check the suggestion in the following reference:

    How do I delete a file which is locked by another process(C#)

    You need to convert the C# code to VB.NET, and if you need further assistance, please let me know.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Young's Friday, June 26, 2020 8:56 AM
    Thursday, June 25, 2020 5:55 AM