using (var file_stream = File.Create("users.xml")) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close(); }
Using the above code works fine. However, when I shorten it to:
var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(File.Create("users.xml"), this);
I get the following exception when I try to deserialize the users.xml file in the same test: The process cannot access the users.xml file because it is being used by another process.
It seems that the reason is that the File.Create method returns an open FileStream, which I cannot close, since I am not holding it by reference.
My bad, or Microsoft ?; -)
Dabblernl
source share