2

Let's say I make a long string called 'lotsofdata', and then output its content with this code:

        string outputFilePath = @"C:\output.txt";
        System.IO.File.WriteAllText(outputFilePath, lotsofdata);

        SpecialFunction1();
        SpecialFunction2();
        SpecialFunction3();

My question is, does the computer completely finish writing all of the stuff to output.txt before moving on to running SpecialFunction1? Or, does it set the outputting process in motion and move on to SpecialFunction1 before the outputting process is complete?

I'm asking because I want to make sure output.txt is done being written to before proceeding to SpecialFunction1() and I don't know how to ensure this.

MrPatterns
  • 4,184
  • 27
  • 65
  • 85

4 Answers4

8

Simple answer is yes.

The underlying stream is filled and closed (the important bit) before the WriteAllText method exits.

File.WriteAllText Method

Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.

http://msdn.microsoft.com/en-us/library/system.io.file.writealltext%28v=vs.110%29.aspx

This is not a golden rule for all file writing. If you were writing directly to a FileStream, you would need to make sure you call Flush or Close (ideally, you should always call Close anyway) if you want to make sure that the file is actually written before continuing.

FileStream.Close Method

Any data previously written to the buffer is copied to the file before the file stream is closed, so it is not necessary to call Flush before invoking Close. Following a call to Close, any operations on the file stream might raise exceptions. After Close has been called once, it does nothing if called again.

http://msdn.microsoft.com/en-us/library/aa328800%28v=vs.71%29.aspx

The key takeaway for you here is that any operation that flushes a stream will not exit until the data has been written to its destination.

Gusdor
  • 14,001
  • 2
  • 52
  • 64
6

Yes, in the code provided first you will finish with the writing text to the file, and only after will run other SpecialFunction functions.

According to the File.WriteAllText documentation:

The file handle is guaranteed to be closed by this method, even if exceptions are raised

So you should not have any concurent IO issues even on big files.

Tigran
  • 61,654
  • 8
  • 86
  • 123
5

System.IO.File.WriteAllText when completes, will flush all the text to the filesystem cache, then, it will be lazily written to the drive.

Alberto
  • 15,626
  • 9
  • 43
  • 56
  • Hello, what does "lazily" written mean? It sounds special, but I have no idea what you're referring to. – MrPatterns Oct 31 '13 at 16:12
  • 2
    @phan 'Lazy' is often used to mean 'processed when it is needed'. What Alberto means is 'asynchronously' or 'in the background'. However, you don't need to be concerned with this because, to your application the filesystem cache and the physical volume contents are the same thing. – Gusdor Oct 31 '13 at 16:23
1

All happening in the same thread, therefore your file will be written before anything else happens.

objecto
  • 54
  • 3
  • 1
    This is not always true when working with IO, it is just true in this specific case. If he had not been using `WriteAllText` and instead used some kind of `StreamWriter` and never closed the file the last bits of text could be sitting in the buffer and not written out to disk. – Scott Chamberlain Oct 31 '13 at 15:16