[Mono-list] Can't open a StreamWritter with a file created by File.Create

RoBiK robik@mailbox.sk
Wed, 2 Jun 2004 17:06:57 +0200


Hi,

It doesn't work, because "File.Create("test-file")" creates a new file =
and
returns an FileStream object, wich holds the handle for the file. You do =
not
assing this object anywhere, so it is a candidate for the garbage
collection. Until the GC disposes this object (this can take some time),
wich releases the allocated file handle, the file is still opened. So if
your next statement tryes to access this file, you get an sharing =
violation.
You should always call Close() to release the underlying file handle.

Solution 1.:

FileStream s =3D File.Create("test-file");
s.Close();
StreamWriter sw =3D new StreamWriter("test-file");
//do some work
sw.Close();

Solution 2 (more elegant):

using(StreamWriter sw =3D new StreamWriter(File.Create("test-file"))) {
	//do some work
}

RoBiK

-----Original Message-----
From: mono-list-admin@lists.ximian.com
[mailto:mono-list-admin@lists.ximian.com] On Behalf Of HannibAl Bundie
Sent: Mittwoch, 02. Juni 2004 16:39
To: mono-list@lists.ximian.com
Subject: [Mono-list] Can't open a StreamWritter with a file created by
File.Create

Hi,

With the mono beta 2 I'm not able to create a file with File.Create and
then, open a StreamWritter on it.
For example a simple program like this throws the error "Unhandled
Exception: System.IO.IOException: Win32 IO returned =
ERROR_SHARING_VIOLATION.

Path: test-file" everytime I run it :

using System;
using System.IO;

namespace Test {

	public class test {

		public test()
		{
			File.Create("test-file");
			StreamWriter sw =3D new StreamWriter("test-file");
		}

		static void Main (string[] args)
		{
			new test();
		}
	}
}

But this kind of operation works perfectly on mono beta 1.


So I would like to know if this bug is a regression (what regression !!) =
or
just a bizzarery of my computer.
Accordingly, I would be grateful if someone could run the little program
above and tell me if he has the same result.
In this case I'll open a bug on bugzilla, and in the other case any help
would be appreciated :)

Thanks in advance

_________________________________________________________________
Bloquez les fen=EAtres pop-up, c'est gratuit ! http://toolbar.msn.fr

_______________________________________________
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list