[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


It doesn't work, because "File.Create("test-file")" creates a new file =
returns an FileStream object, wich holds the handle for the file. You do =
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 =
You should always call Close() to release the underlying file handle.

Solution 1.:

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

Solution 2 (more elegant):

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


-----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


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 =

Path: test-file" everytime I run it :

using System;
using System.IO;

namespace Test {

	public class test {

		public test()
			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 !!) =
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