0

I made sequence of operations (on local disk):

  1. Copy file to ###.bak
  2. Save file to ###
  3. Delete file ###.bak

Code:

File.Copy(filename, filename + ".bak");
TextWriter writer = new StreamWriter(filename);
writer.Write(content);
writer.Close();
File.Delete(filename + ".bak");

During deleting I got exception:

IOException: There is not enough space on the disk.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)

Size of both files is maximum few MB. And I checked - there is enough free space on disk (more than few GBs). What are possible reasons for that situations?

It's a desktop app (WPF), running on OS drive, on admin account.

8
  • Could you show us some code, please? Wich method do you use to delete the files? System.IO.File.Delete? Commented Mar 5, 2012 at 10:29
  • Code is posted. Yes, I used System.IO.File.Delete Commented Mar 5, 2012 at 10:38
  • I suggest you to check a permission exception. Commented Mar 5, 2012 at 10:41
  • 2
    Try to narrow on the problem: (1) make 200% sure you get your file sizes and free disk space right. (2) make 200% sure you're filename is the file you think it is. (3) try your code with really small files (e.g. <1KB in size) and see if the error still happens (4) try to copy the file manually using explorer or cmd.exe/copy and see if the error still happens. Commented Mar 5, 2012 at 10:51
  • 3
    I think you are not getting the error on File.Delete, cause File.Delete calls internally Win32Native.DeleteFile. This exception is raised on File.Copy, cause this method calls File.InternalCopy as listed in your StackTrace Commented Mar 5, 2012 at 11:10

3 Answers 3

0

Here I will suggest two thing as per below :

  1. Use using keyword to closed write object before delete.

    File.Copy(filename, filename + ".bak");
    using (TextWriter writer = new StreamWriter(filename))
    {
        writer.Write(content);
    }
    if (File.Exists(filename + ".bak"))
    {
        try
        {
            File.Delete(filename + ".bak");
        }
        catch (IOException ex)
        {
            Debug.WriteLine("Delete failed: " + ex.Message);
            Thread.Sleep(500); // allow file system to catch up
            File.Delete(filename + ".bak"); // retry
        }
    }
    
  2. Also I faced issue is file corrupted when copy from source like FTP So please check before delete you just copy file on destination folder and check is corrupted or not and able to open if are able to open same location then try delete operation.

Sure it will work..

Sign up to request clarification or add additional context in comments.

Comments

-1

Are you making this operation on an hard drive different from the OS one? Maybe you have lot of space in drive D, but when deleting the file you're actually moving it in the recycle-bin, that is in C drive. Try to check this...

3 Comments

Isn't there a separate "Recycle Bin" folder ("RECYCLER" root directory) per drive? I though so. But regardless, File.Delete does not use the Recycle Bin at all.
I thought it uses the recycle bin because of the strack trace he printed out... third line at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) makes me think about copying file to recycle bin...
Well, that could be the case, but I doubt it. Because even if the RecycleBin stuff does a copy somewhere internally and would be used by File.Delete, it is unlikely it is done inside .NET, but would be in bowels of the OS/shell. Also, that the stack is the result of the actual File.Copy seems much more likely to me :-)
-1

Are you using disk quotas? If yes, maybe on the server that it failed on, that account was using a lot of disk at that time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.