Can't write to file c#
<p>I have a loop which scans for xml files, when success it get in to loop and manipulate strings. Problem is i can't write post because i got warning file in use. How do i fix this?</p>

<p>Loop:</p>

<pre><code>var dir = new DirectoryInfo(@"C:\Projektdata");
foreach (var file in dir.EnumerateFiles("*.xml", SearchOption.AllDirectories))
{            
    foreach (string line in File.ReadLines(file.DirectoryName + @"\"  + file))
    { 
      . . . .
</code></pre>

<p>Section to write file:</p>

<pre><code>string directoryPath = file.DirectoryName + @"\" + file;

StreamWriter writer = new StreamWriter(directoryPath);

writer.WriteLine(source);                                              

writer.Close(); 
</code></pre>