Vb net streamwriter the process cannot access the file




















First, I'm creating a tab delimited text file in one screen, and writing a 'header' line as its first line, then 'closing' and 'disposing' of both the FileStream and StreamWriter. I'm getting the 'cannot access the file Additional background info: the data is collected from a serial port based on a system.

I think I see what's happening. In the second part, you are creating a new StreamWriter that is opening the file directly instead of creating a FileStream that gets passed to a StreamWriter as you did in the first part. When you do this, the second part of the code blocks the first. It doesn't share access to the file.

Also, if the first file still has it open, the second part tries to open it without share access but it can't because another file is using it.

The simple solution is to change your second part to load the file using a filestream with the FileShare. ReadWrite flag set. This way, both sections of code get access to the same file. You don't even have to close and dispose of the file. You can keep it open in both locations if you wish. You can't close the first stream from the second because the first is used in a timer, which puts in in another thread. You can't modify objects declared in one thread from another thread not safely, or easily anyway.

But if you do the filestream in the second part, you shouldn't have to try to close the first stream at all! The first thing I would try is on both streams change the FileShare. Write to FileShare. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.

Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 0. Related 1. Hot Network Questions. OK Paste as. Treat my content as plain text, not as HTML. Existing Members Sign in to your account. This email is in use. Do you need your password? Submit your solution! When answering a question please: Read the question carefully. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.

If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.

Related Questions. Any idea why it is showing the process cannot access the file because it is being used by another process? Layout: fixed fluid. Try using File. Open fileName, FileMode. Open, FileAccess. Read, FileShare. None ;. Ravi Vanapalli Ravi Vanapalli 9, 3 3 gold badges 30 30 silver badges 41 41 bronze badges.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back



0コメント

  • 1000 / 1000