Results 1 to 6 of 6

Thread: [RESOLVED] stuck trying to delete files - is being used by another process

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Resolved [RESOLVED] stuck trying to delete files - is being used by another process

    okay so my problem is that i am reading information from XML's,

    and i also have a delete button for some that i want to remove after getting the proper information.
    tho i am having an error deleting the file after i got the information from inside, saying 'it is being used by another process':

    there is no chance another app is accessing that same file, it must be a problem within my code!

    System.IO.IOException: 'The process cannot access the file 'FilePath.xml' because it is being used by another process.'

    here is the code i am using the read information from XML, while avoiding encoding problems and 'case sensitive' tags.

    i have tried putting

    Stream.Close
    and also
    textReader.Close

    but that did not solve my problem.


    Code:
    Public Function RipXML(ByRef x As String, y As String)
    
            Dim Stream As New IO.StreamReader(x, System.Text.Encoding.UTF8)
            Dim textReader As New Xml.XmlTextReader(Stream)
            Dim lastElementName As String = ""
            While textReader.Read()
                Select Case textReader.NodeType
                    Case Xml.XmlNodeType.Element
                        lastElementName = textReader.Name
                    Case Xml.XmlNodeType.Text
                        If lastElementName.ToLower = y.ToLower Then
                            y = textReader.Value.Trim
                            Return y
                        End If
                End Select
            End While
    End Function
    trying to delete/;

    Code:
    My.Computer.FileSystem.DeleteFile(FilePath)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: stuck trying to delete files - is being used by another process

    So much wrong with that code. The issue is that you're opening the file but not closing it, but there's plenty more that should be fixed. Firstly, your method has no return type. Secondly, you're declaring a parameter ByRef for no apparent reason. Your parameters are also appallingly-named and you're setting one of them for no apparent reason as well.
    vb.net Code:
    1. Public Function RipXml(filePath As String, elementName As String) As String
    2.     Using textReader As New StreamReader(filePath, Encoding.UTF8),
    3.           xmlReader As New XmlTextReader(textReader)
    4.         Dim lastElementName = String.Empty
    5.  
    6.         While xmlReader.Read()
    7.             Select Case xmlReader.NodeType
    8.                 Case XmlNodeType.Element
    9.                     lastElementName = xmlReader.Name
    10.                 Case XmlNodeType.Text
    11.                     If String.Equals(lastElementName, elementName, StringComparison.InvariantCultureIgnoreCase) Then
    12.                         Return xmlReader.Value.Trim()
    13.                     End If
    14.             End Select
    15.         End While
    16.  
    17.         Return Nothing
    18.     End Using
    19. End Function
    The method name is poor too. I look at that and I have no idea what the method actually does, other than that it presumably gets something out of some XML. GetXmlElementTextByName or the like would be more appropriate. Self-documenting identifiers are almost always better.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Re: stuck trying to delete files - is being used by another process

    oh wow, thank you for your comment.

    apparently i need to spend some more time understanding things to their fullest.

    your changed indeed solve my problems, i will look more into it!

    once again, thank you!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: stuck trying to delete files - is being used by another process

    One thing I would suggest that you do immediately is turn Option Strict On, both in the project properties and also in the IDE options, so that it is On by default in future projects. That will prevent your doing things like declaring functions with no return type and it will help you write better code by paying more attention to your data types.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Re: stuck trying to delete files - is being used by another process

    as a matter of fact it was turned on the whole time!

    edit:
    or was it.. actually it was Off the whole time

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: stuck trying to delete files - is being used by another process

    Quote Originally Posted by ShAAAMen View Post
    actually it was Off the whole time
    Yeah, I knew that it had to be because you couldn't have a function with no return type otherwise. There are reasons that it is Off by default but most experienced VB.NET programmers wish it was On by default.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width