2

I use File.WriteAllBytes to create a file "somefile.txt". Does it mean that when File.WriteAllBytes returns from execution the bytes passed to File.WriteAllBytes are actually on the disk? For example I write X amount of bytes with File.WriteAllBytes("somefile.txt", bytes) and then check the size of the file on the disk if the size equals the bytes.length. If it equals I assume the file was completely written to disk.

Say the bytes size is 1.5GB and on the machine it takes a while to write such amount; the disk where the file is written is a mapped network drive. Is it possible that File.WriteAllBytes returns from execution, but the OS is still writing bytes to the disk?

  • I don't think so, as `File.WriteAllBytes` **closes** the file after writing the bytes – Pikoh Apr 28 '16 at 07:04
  • @Pikoh Does the method close() wait while the bytes passed are actually written to the disk? – Developer Marius Žilėnas Apr 28 '16 at 07:10
  • I think close first copies all the data written to the buffer to disk, and after that releases all the resources. I don't believe it has a way to check it bytes are fisically written, but i think they are. – Pikoh Apr 28 '16 at 07:14
  • Anyway..why are you asking this? If you have a specific problem, i think you should ask about that problem – Pikoh Apr 28 '16 at 07:16
  • @Pikoh I am writing files to a mapped network drive. To show that the file is written completely I immediately after call to File.WriteAllBytes take DateTime.Now and put it to property WritingEnded. Here I **assume** that after File.WriteAllBytes returns the bytes are actually on the disk, i.e. WritingHasEnded. *I want to get rid of that assumption* :). – Developer Marius Žilėnas Apr 28 '16 at 07:18

1 Answers1

2

Yes, it's totally normal that the OS may (or may not) actually be still writing to the disk after finishing writing within your programming API. That's literally not your concern, though. If you ask for the size you'll get the expected and correct size that you have written, even if your OS utilized a write cache. Those processes are abstracted by the OS for you, so don't worry.

But as you mentioned network drives in your comment: This is not the case with network drives usually, though. Because the OS can't simply use a write cache and finish the write execution asynchronous. (Network may go down unexpected etc.)

Num Lock
  • 742
  • 1
  • 6
  • 32
  • Ty for the answer. It is a mapped network drive. In this case I have not to worry about incorrect size? :) – Developer Marius Žilėnas Apr 29 '16 at 06:43
  • 1
    If your OS tells you it finished writing your data, without yielding any errors, you can expect it did so. – Num Lock Apr 29 '16 at 07:13
  • To be paranoid I just have to have use FileInfo and get file size after the control returns from execution of File.WriteAllBytes. :-F At least File.WriteAllBytes is synchronous (at least it says there http://stackoverflow.com/a/16746598/1737819 , http://stackoverflow.com/a/15467185/1737819 ) and it blocks execution for some time. – Developer Marius Žilėnas Apr 29 '16 at 07:30