FlyBoy Posted September 14, 2004 Posted September 14, 2004 in my form_load sub...im trying to make a procedure which reads all the user names from a file using the streamreader and then putting them all into a listbox. all works fine...unless when the file does not exitst. so i tried the following thing(in the form load): if file.exist("c:\1.txt")=false then file.create("c:\1.txt") else dim fs as new filestream("c:\1.txt") ** the file to listbox procedure ** end if then when im trying to get into the file again,to read or write from the program it says that its already being used....(i guess by the "file.create" actually im sure..) any idea how to release it from this file.create??? Quote
Administrators PlausiblyDamp Posted September 14, 2004 Administrators Posted September 14, 2004 You could just use Dim fs As New FileStream("c:\1.txt", FileMode.OpenOrCreate) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
FlyBoy Posted September 14, 2004 Author Posted September 14, 2004 You could just use Dim fs As New FileStream("c:\1.txt", FileMode.OpenOrCreate) 10x ;) ;) ;) Quote
georgepatotk Posted September 15, 2004 Posted September 15, 2004 write this code at the end of the function or precedure for releasing resources System.GC.Collect. Quote George C.K. Low
Arch4ngel Posted September 15, 2004 Posted September 15, 2004 This is not a good idea. This is really unefficient programming. Doing this might cause some slowdown if there's a lot to release. Better let the framework do the job when needed. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
georgepatotk Posted September 16, 2004 Posted September 16, 2004 I didn't see any issues on efficiency asked from FlyBoy. Why is it not implementable? anyway, FlyBoy, you can do fs.Close after the codes for releasing the resource on file. I was just giving answer based on the question.. hehe... Quote George C.K. Low
Administrators PlausiblyDamp Posted September 16, 2004 Administrators Posted September 16, 2004 Generally speaking you are better letting .Net manage the Garbage Collection process - it pretty much tunes itself to the available resources / memory usage patterns of your application and forcing a collection can interfere with this. If you really must for a collection then GC.Collect() forces a full gartbage collection which can take a long time to complete and also have the side effect that some resources may now take longer to be released than if you hadn't forced collection, trying something like GC.Collect(0) may be a bit easier on the system. A better plan is that if a class exposes a Close / Dispose method then calling that function when you are finished with the object will release critical resources, close the file / db connection / etc Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jay1b Posted September 16, 2004 Posted September 16, 2004 Sorry to hijack the thread. But whats the difference between Filestream and Streamreader? Quote
HJB417 Posted September 17, 2004 Posted September 17, 2004 in my form_load sub...im trying to make a procedure which reads all the user names from a file using the streamreader and then putting them all into a listbox. all works fine...unless when the file does not exitst. so i tried the following thing(in the form load): if file.exist("c:\1.txt")=false then file.create("c:\1.txt") else dim fs as new filestream("c:\1.txt") ** the file to listbox procedure ** end if then when im trying to get into the file again,to read or write from the program it says that its already being used....(i guess by the "file.create" actually im sure..) any idea how to release it from this file.create??? File.Create(String) Returns a Stream, in the code provided, you don't close the stream returned to you, and that's why the file wasn't 'released'. No amount of calls to GC.Collect will solve the problem. Quote
HJB417 Posted September 17, 2004 Posted September 17, 2004 Sorry to hijack the thread. But whats the difference between Filestream and Streamreader? Ripped from the docs StreamReader is designed for character input in a particular encoding' date=' whereas the Stream class is designed for byte input and output. [/quote'] So if you're going to be reading from a stream that you know is text, one would most likely use a StreamReader over a FileStream. Quote
TamuTamu Posted October 4, 2004 Posted October 4, 2004 This is C# but I figure the code should be roughly the same for VB.NET. Whenever I deal with filestreams I always use the same structure of code: FileStream fs = null; TextWriter writer = null; try { fs = new FileStream(somePath, FileMode.Create); writer = new StreamWriter(fs); writer.Write("Some Stuff"); /// put more useful stuff here } finally { if(writer!= null) { writer.Close(); } if(fs!=null) { fs.Close(); } } No matter what happens in the code the stream will be closed as it is within the finally braces. The error will no longer occur, additionally if you have the problem with one of your files in windows if you log out and log back in it will clear all the filestreams. Quote
HJB417 Posted October 4, 2004 Posted October 4, 2004 This is C# but I figure the code should be roughly the same for VB.NET. Whenever I deal with filestreams I always use the same structure of code: FileStream fs = null; TextWriter writer = null; try { fs = new FileStream(somePath, FileMode.Create); writer = new StreamWriter(fs); writer.Write("Some Stuff"); /// put more useful stuff here } finally { if(writer!= null) { writer.Close(); } if(fs!=null) { fs.Close(); } } No matter what happens in the code the stream will be closed as it is within the finally braces. The error will no longer occur, additionally if you have the problem with one of your files in windows if you log out and log back in it will clear all the filestreams. Your sample can be further simplified to: TextWriter file = File.Create(somePath); try { file.WriteLine("Some Stuff"); file.Flush(); } finally { file.Close(); } There is no need to declare 'file' as null outside of the try{} block and then instantiate it inside the try{} block because if File.Create fails/throws an exception, there will be no stream for the caller to close so I instantiate it outside of the try{} block because file would have to be a valid instance of a TextWriter to enter the try{} block. It's usually good practice to call flush before close but in this case, flush is called by the StreamReader when Close() is called, so in my example, the call to flush is redundant and can removed. Creating a StreamWriter by passing an instance of a FileStream and then calling Close on both the FIleStream and StreamWriter is also redundant as the StreamWriter class already does this for you. Minor things, but it produces slightly more effecient, but more importantly, more readable code. Quote
Administrators PlausiblyDamp Posted October 5, 2004 Administrators Posted October 5, 2004 or if you want to get even more compact ;) using(TextWriter file = File.Create(somePath)) { file.WriteLine("Some Stuff"); file.Flush(); } and even the Flush is probably un-needed. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
TamuTamu Posted October 5, 2004 Posted October 5, 2004 Cool, that'll save me some typing in the future, thanks :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.