none
"Exception thrown" when trying to delete a file?

    Question

  • I'm trying to update an old VS2013 app I wrote using VS2017, but I keep running into problems. :(

    I'm trying to delete a file using the FileSystem object:

    FileSystem.Kill(foundFile)

    ...where foundfile = "D:\Path\filename.ext". The target file (nor path) is not Hidden nor protected yet my program never deletes the file, instead refusing to move on (I put a breakpoint on the line, step into it, the target file is not deleted and execution does not continue.)


    If I stop execution, the Output window reports two mysterious "Exception thrown" errors:

    Exception thrown: 'System.IO.IOException' in mscorlib.dll
    Exception thrown: 'System.IO.IOException' in Microsoft.VisualBasic.dll
    I have Imports System.IO in my declarations. The Microsoft Scripting Runtime is added through the Reference Manager. No clue what these errors mean or how to fix them. I never had this problem with VS2013.

    Thx.
    Thursday, April 06, 2017 5:19 PM

Answers

  • Hello,

    Try the following, let us know what the error message is. The IOException requires and Import statement for System.IO in the file which uses the code below. 

    Try
        If File.Exists(foundFile) Then
            File.Delete(foundFile)
        End If
    Catch ex As IOException
        MessageBox.Show("Encountered an exeption removing " &
                foundFile & Environment.NewLine & ex.Message)
    End Try
    A common reason for this is that the file is "in use by another process".


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, April 07, 2017 9:17 AM
    Moderator

All replies

  • Hi Mugsy_in_Houston,

    Welcome to the MSDN forum.

    Refer to your description, your issue is relating to the VB development. Since our forum is to discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor, I will help you move it to the appropriate forum: Visual Studio Languages  >  Visual Basic for dedicated information, you will get a more professional support from there, thank you for your understanding.

    Best regards,

    Sara


    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.

    Friday, April 07, 2017 6:04 AM
  • Hi Mugsy_in_Houston,

    According to your description, I guess it might be the mscorlib.dll and Microsoft.VisualBasic.dll problem, please check if these file path are right.
    Please left-click Reference ---Double-click any one reference, then you can see Object Browser form, please check mscorlib.dll and Microsoft.VisualBasic.dll file are present.

    If you cannot find these dll in your computer file, you can copy these dll to your computer and rebuild your project again.
    Best Regards,
    Cherry Bu


    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.

    Friday, April 07, 2017 6:12 AM
    Moderator
  • I'm trying to update an old VS2013 app I wrote using VS2017, but I keep running into problems. :(

    Use the File.Delete method instead. It might not delete the file, but it will give you a much more useful error message.

    https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx

    Friday, April 07, 2017 6:33 AM
  • Hello,

    Try the following, let us know what the error message is. The IOException requires and Import statement for System.IO in the file which uses the code below. 

    Try
        If File.Exists(foundFile) Then
            File.Delete(foundFile)
        End If
    Catch ex As IOException
        MessageBox.Show("Encountered an exeption removing " &
                foundFile & Environment.NewLine & ex.Message)
    End Try
    A common reason for this is that the file is "in use by another process".


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, April 07, 2017 9:17 AM
    Moderator
  • Do you have somewhere this kind of code in your program

    Try
    'Do Something
    Catch
    'Don't handle it
    End Try

    It can throw exceptions on the strangest places


    Success
    Cor

    Friday, April 07, 2017 9:52 AM
  • Ah, you are correct. The first file it tries to delete is indeed "open in another application"... or so Windows claims when I try to delete it by hand.

    Checking the Task Manager, the program that supposedly has it open ("CaptureGenUSB.exe") does not appear in the Task Manager (not as an Application, Process or Service) so the file shouldn't be in use but is which is why it caught me by surprise. ("CaptureGenUSB.exe" is part of my USB TV tuner.)

    I'll use a Try/Catch to skip "in use" files. Thx.

    Friday, April 07, 2017 11:23 AM
  • Thanks to everyone for their help.

    I didn't have a Try/Catch, but will need one now. :)

    Friday, April 07, 2017 11:26 AM
  • In fact I discovered that to be in able to delete a text file it matters how you create the file.

        'Create file  
      Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            If ListBox1.SelectedItem <> "" AndAlso TextBox1.Text <> "" Then
                'File.Create(DirPath + "\" + ListBox1.SelectedItem + "\" + TextBox1.Text)
                File.WriteAllText(DirPath + "\" + ListBox1.SelectedItem + "\" + TextBox1.Text, " ")
            End If
        End Sub

    If you use File.Create() (commented above) then you hardly can delete it. In fact you can, after a while or after processing an other file/dir or after closing and reopening you app. Even if you never open this file, it seems open by vshost32.exe process.

    Use File.WriteAllText() instead which will create a new file, and then close it so you can delete it.

    Wednesday, February 21, 2018 1:48 PM
  • If you use File.Create() (commented above) then you hardly can delete it. In fact you can, after a while or after processing an other file/dir or after closing and reopening you app.

    https://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx 
    "The file is opened with read/write access and must be closed before it can be opened by another application."

    Wednesday, February 21, 2018 8:12 PM
  • Yes. I didn't see the line you quoted at first look. 

    • Edited by eta-sys Friday, February 23, 2018 10:08 AM
    Friday, February 23, 2018 7:35 AM