I have written a class that stores user options throughout the program. (Color preferences, startup windows, etc.) When the program initializes, a FileStream object is used to open then file then a binary formatter is used to deserialize the stream. The FileStream is then closed.
The problem is that when the program closes and the FileStream object trys to open the file to save the updated user options. A file exception is sometimes threw: "Cannot access file because it is being used by another proccess". Whats so annoying about the error is that it only happens SOMETIMES! A debugging nightmare! I assume that the object handle is not being released somewhere in memory. Can anybody give me any pointers on overcoming this problem?
FileStream error. ????
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

AlanRank #669 · 3.6K reputationPosted Jul 6, 2007, 11:28 AM EDT
retry:
try
{
// code to open filestream
}
catch
{
goto retry;
}
Jordi24 reputationPosted Jul 13, 2007, 7:36 AM EDT
Perhaps you are trying to access a file being used already.
If you programatically read a CSV file that is open and you already had an Excel spreadsheet open of that CSV file, you'd get the results you report.
Try closing all files down first.