-
Oct 28th, 2020, 08:43 PM
#1
Thread Starter
New Member
[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)
-
Oct 28th, 2020, 09:13 PM
#2
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:
Public Function RipXml(filePath As String, elementName As String) As String
Using textReader As New StreamReader(filePath, Encoding.UTF8),
xmlReader As New XmlTextReader(textReader)
Dim lastElementName = String.Empty
While xmlReader.Read()
Select Case xmlReader.NodeType
Case XmlNodeType.Element
lastElementName = xmlReader.Name
Case XmlNodeType.Text
If String.Equals(lastElementName, elementName, StringComparison.InvariantCultureIgnoreCase) Then
Return xmlReader.Value.Trim()
End If
End Select
End While
Return Nothing
End Using
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.
-
Oct 28th, 2020, 09:53 PM
#3
Thread Starter
New Member
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!
-
Oct 28th, 2020, 09:56 PM
#4
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.
-
Oct 28th, 2020, 11:08 PM
#5
Thread Starter
New Member
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
-
Oct 28th, 2020, 11:18 PM
#6
Re: stuck trying to delete files - is being used by another process
Originally Posted by ShAAAMen
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|