file in use

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
8 posts Page 1 of 1
Contributors
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

file in use
Dummy1912
hello,

can anyone help me with this part also
Code: Select all
        Dim Files As String() = Directory.GetFiles("c:\", "*.txt")
        For Each sFile As String In Files
            Dim fi As New FileInfo(sFile)
            Dim myFileStream As New System.IO.FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
            Dim myReader As New System.IO.StreamReader(myFileStream)
            Dim sFileContents As String = myReader.ReadToEnd()
            myReader.Close()
            myFileStream.Close()

the file stays in use so i can't delete it
what are we doing wrong?

thanks
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: file in use
Shim
close your visual studio or close the project then delete it.
Find my programs on Softpedia
User avatar
Ffenixw0rks
VIP - Donator
VIP - Donator
Posts: 152
Joined: Sun Jun 19, 2011 2:51 pm

Re: file in use
Ffenixw0rks
I can't recreate your error using vs2013.
Haven't you opened file in other program? And can you please give some more info. Like on what line does exception occurs.

Edit: you can change
Code: Select all
Dim fi As New FileInfo(sFile)
Dim myFileStream As New System.IO.FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
Dim myReader As New System.IO.StreamReader(myFileStream)
to
Code: Select all
Dim fi As New FileInfo(sFile)
Dim myReader As New System.IO.StreamReader(fi.FullName)
As StreamReader also can take file path with name as argument. Then myFileStream.Close() is also not needed.
Last edited by Ffenixw0rks on Mon Jun 09, 2014 5:51 pm, edited 2 times in total.
Image
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: file in use
XTechVB
Why do you use the "FileStream" and "StreamReader" to read text based files, and not the built-in functions?
You can find me on Facebook or on Skype mihai_92b
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: file in use
Dummy1912
Shim wrote:
close your visual studio or close the project then delete it.
#Shim :lol: because i need to delete it when i run the app itself
Ffenixw0rks wrote:
I can't recreate your error using vs2013.
Haven't you opened file in other program? And can you please give some more info. Like on what line does exception occurs.

Edit: you can change
Code: Select all
Dim fi As New FileInfo(sFile)
Dim myFileStream As New System.IO.FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
Dim myReader As New System.IO.StreamReader(myFileStream)
to
Code: Select all
Dim fi As New FileInfo(sFile)
Dim myReader As New System.IO.StreamReader(fi.FullName)
As StreamReader also can take file path with name as argument. Then myFileStream.Close() is also not needed.
#Ffenixw0rks no this is the only app that use the file
changed it but same error
Access to the path 'c:\oft.txt' is denied.


XTechVB wrote:
Why do you use the "FileStream" and "StreamReader" to read text based files, and not the built-in functions?
#XTechVB no idea whats the different between them?
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: file in use
XTechVB
Dummy1912 wrote:
#XTechVB no idea whats the different between them?
Well, the built-in finctions like IO.File.ReadAllText(), IO.File.ReadAllLines() were specifically created to avoid this type of problems you are having.
That's why they close the files automatically, making them available for other uses.
You can find me on Facebook or on Skype mihai_92b
User avatar
Ffenixw0rks
VIP - Donator
VIP - Donator
Posts: 152
Joined: Sun Jun 19, 2011 2:51 pm

Re: file in use
Ffenixw0rks
#Dummy1912 if error will still occur - try to logout and log in from Windows.
Another way is to use Unlocker to see what app is using file at the moment. cooll;
Image
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: file in use
Dummy1912
thank you guys

working now
weird :lol:
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
8 posts Page 1 of 1
Return to “Coding Help & Support”