문제

There is a code:

using (var sw1 = new StreamWriter(filename))
{
    var sw2 = new StreamWriter(sw1.base as FileStream);
    while (...)
       sw2.WriteLine(...); //a lot of lines are written
}

Can it lose some data without explicit flushing of sw2? According to MSDN (as I remember) internal stream should not be disposed directly, that's why I doesn't cover sw2 by using also.

Why do I need sw1? Cause it's just example and I have same situation in my project as a result of its architecture (sw1 is created out of a function with sw2 and should be passed as FileStream due to an interface).

도움이 되었습니까?

해결책

Obviously, yes, it can. To prevent data losing sw2 should be flushed before writing to sw1 will be continued. Also sw2 should not be used after sw1 disposing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top