Normally, I wouldn't post a second solution, but in this case, now you have posted your code there is a lot to discuss here...
First off, that isn't C# code - it's VB. Always tag your language correctly or you will confuse yourself when the replies don't work or even compile!
Secondly - and I hate to say it - that code looks like it was thrown together by someone who hadn't thought at all, just hoped it would do something, anything, compile-you-bugger, run, oh-it-don't-work, panic.
You have to look at what methods do and what they return, and think about how you need to use them.
You are having a problem that a file is in use: of course it is - you open it twice and don't close either of them!
File.Open(f.FullName, FileMode.Open)
Start with the documentation of File.Open:
File.Open Method (System.IO) | Microsoft Docs[
^] and it says very clearly what it does:
Quote:
Returns
FileStream
A FileStream opened in the specified mode and path, with read/write access and not shared.
So why do you ignore that FileStream and try to create your own in the next line?
Dim swWriter As New StreamWriter(f.FullName)
That's going to fail, because you have just opened the file for exclusive access in the preceding line!
Replace the two lines with this:
Using swWriter As StreamWriter = File.Open(f.FullName, FileMode.Open)
...
End Using
And put your code to write the file inside where the ellipsis is.
The
Using
block will automatically close the file when you are finished with it, and the variable
swWriter
goes out of scope. After the
Using
block closes, you can delete your file, safe in the knowledge that it is no longer in use.
Give that a try, and see how much further you get.
And please, sit down and think before you rush into code - it will save you a huge amount of time and effort!