Click to See Complete Forum and Search --> : Strange IOException at FileStream.Write call


ded.diman
September 17th, 2008, 07:06 AM
Hi all.
I have a problem while trying to copy a file to the drive. For copying I use FileStream next way:


FileStream dstStream = new FileStream(dstFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

int bytesToCopy = ...; //something appropriate
byte[] buffer = ...; //something appropriate

dstStream.Write(buffer, 0, bytesToCopy);


And it works fine on the whole. But only while the size of the file is greatly less than drive free space size (for proper work it should be less on 100-200 Kb at least). But if the size of the file is less than free space size only on 10-40 Kb than at FileStream.Write() call I have a System.IO.IOException. It is strange because file size is less than available free space. And copying of this file by Windows Explorer or different file managers causes no problem.

I tried to create FileStream instance by constructors where buffer size and option FileOptions.WriteThrough are specified. But it didn't help. :(

Maybe someone knows the reason of such behaviour and/or how it could be fixed?
Thanks.

boudino
September 17th, 2008, 08:27 AM
Don't forget, that files are allocated by cluster. The cluster is smallest possible unit which is allocatable on the disk, typicaly it is 512 bytes. Even if the size of the file is several bytes, all 512 bytes is allocated on the disk. This can cause that the whole file content cannot be writen to the disk if the disk is almost full.

ded.diman
September 17th, 2008, 09:09 AM
Thank you for your answer!
But it seems to me that problem is not in the cluster size.

Now disk has got 240 Kb of free space. The size of file to be copied is 231 Kb. So after copying is completed it will be about 9 Kb free. You see that it is rather more than 512 bytes.

And still, this file is successfully copied by Windows Explorer and some file managers. So it is really possible to copy it.

ded.diman
September 18th, 2008, 03:55 AM
I've found that it is not problem of .NET only. My attempts to copy file in different ways completed with the same result.

I tried to copy file in these ways:

1) In the .NET: System.IO.File.Copy();
2) In Win API: SHFileOperation();
3) In the low level C++: fopen(); ... fwrite(); ... fclose().

And every time I had an exception or fail at operation execution.

As it's not .NET problem I'll try to ask in C++ section of forum.