CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    4

    Unhappy Strange IOException at FileStream.Write call

    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.

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Strange IOException at FileStream.Write call

    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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Sep 2008
    Posts
    4

    Re: Strange IOException at FileStream.Write call

    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.

  4. #4
    Join Date
    Sep 2008
    Posts
    4

    Re: Strange IOException at FileStream.Write call

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured