3

When refreshing my application, I have to read from a text file which has information stored in it. After it is read I make sure I have closed it 'file.close()' the next time I refresh the applicaztion the text file should be generated again and overright the previous one.

The problem is that it cant change the file because it is aparantly in use by another process but as soon as I close my application it works fine. Is there anyway to stop the process before refreshing it so it works correctly? The text file is called NetworkInfo.txt.

Thanks Chris

EDIT

Every time I refresh the application I run a batch file that generates a file with all the network info in. (IPConfig/all)

I then have a module that reads from it like the following:

Public Function EthDefaultGateway() As String Dim sr As New System.IO.StreamReader(ipconfig) Try Dim foundEthernet As Boolean = False Dim gateway As String = "" Do Until sr.EndOfStream Dim line As String = sr.ReadLine() If line.Contains("Ethernet adapter LAN:") OrElse line.Contains("Ethernet adapter Local Area Connection:") Then foundEthernet = True End If If foundEthernet Then If line.Contains("Default Gateway . . . . . . . . . :") Then gateway = line.Substring(line.IndexOf(":") + 1).Trim Exit Do End If End If Loop If gateway = "" Then gateway = "Unknown" End If If gateway = "::" Then gateway = "Unknown" End If Return gateway Catch ex As Exception EthDefaultGateway = "Unknown" End Try sr.Close() sr.Dispose() End Function 

From this I gather all the bits of information I need. (There probs is a lot better way of doing this but Im only a nooby and I cant find any other ideas on here, the web or from friends.)

All of these close the file after readinf from it (sr.close)

Some reason though its not closing it. The only other thing It could be is the batch file isnt closing it which I think is very unlikely

The problem is that when I change the IP or refresh the form it fails because the batch file cannot over write the network info file.

Any suggestions?

I was just thinking of a way to kill the process after the refresh but I didnt know if this was a good idea or if it was doable or how to do it tbh.

12
  • Can you post some code? It's hard to provide an answer when we can't see what it is you're doing. It sounds like you're not disposing the stream properly, but without any code it's hard to say. Commented Aug 15, 2012 at 22:42
  • Post updated. Sorry for not posting code:P Commented Aug 15, 2012 at 22:51
  • the text file should be generated again and overright the previous one I think we might need to see this code, too. Commented Aug 15, 2012 at 22:55
  • 1
    sr.close() only closes the file; it doesn't release the StreamReader, so it's being retained until GC (garbage collection) cleans it up. Set sr to nothing after the close - not a vb.net guy, so I won't post an answer showing how. In C#, you'd put the code in a using block to auto-dispose of the stream, but I'm not sure how you'd do the same in vb.net. Commented Aug 15, 2012 at 22:56
  • 1
    You need to move your Return gateway to after the sr.Dispose() - you're returning from the function before it gets to the part that closes and disposes of the StreamReader. Commented Aug 15, 2012 at 23:58

1 Answer 1

6
 Return gateway 

That bypasses the sr.Close() call at the bottom. So the file won't be closed. Always favor using the Using statement so it is automatic and can't be forgotten or skipped or bypassed because of an exception:

Public Function EthDefaultGateway() As String Using sr As New System.IO.StreamReader(ipconfig) '' Rest of your code End Using End Function 
Sign up to request clarification or add additional context in comments.

2 Comments

+1. This handles both the Dispose() issue and the early return.
Thanks. After pratting around with the code and installer,I managed to kill the program haha. Good job I have backups:P Il give this ago once I get it up running again. Seems like it will fix the problem. (The strange thing is that Im using one of my old backups. Very little has changed and yet the problem doesnt happen. looks up info from same file in same way but still no errors. Either way. Will be a good idea to get this fixed. Thanks again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.