title: w3wp.exe (NETWORK SERVICE user) is locking a newly created file and preventing access to it
body: <p>I have a Web application.  It sends a series of images to the server (no problem there) and then uses code from <a href="http://msdn.microsoft.com/en-us/library/bb332455%28v=office.12%29.aspx" rel="nofollow">this tutorial</a> to create a PowerPoint presentation.  The presentation is saved in a directory on the web server and the URL is returned to the user.</p>
  
  <p>However, the file is still in use and attempting to access it generates a 500.0 error in IIS 7.5.  </p>
  
  <p>If I open the task manager and kill the w3wp.exe process that belongs to the NETWORK SERVICE user everything works as intended (the file can be downloaded and viewed).  I also have another w3wp process belonging to DefaultAppPool, but it doesn't seem to cause a problem.  </p>
  
  <p>I'm new to this .Net coding, so it is very possible I forgot to close something down in code.  Any ideas?</p>
  
  <p><strong>Edit</strong>:  This is the method that creates a series of png's from image data that is encoded into a string.  It uses a Guid to create a unique bit of a directory path, and checks to make sure it doesn't exist and then creates the directory and places the images there.</p>
  
  <p>It looks like the offending method is this one:</p>
  
  <p>So the offending method is this one:</p>
  
  <pre><code>    public void createImages(List&lt;String&gt; imageStrings)
      {
          UTF8Encoding encoding = new UTF8Encoding();
          Decoder decoder = encoding.GetDecoder();
  
          Guid id = Guid.NewGuid();
          String idString = id.ToString().Substring(0, 8);
  
          while (Directory.Exists(imageStorageRoot + idString))
          {
              id = Guid.NewGuid();
              idString = id.ToString().Substring(0, 8);
          }
  
          String imageDirectoryPath = imageStorageRoot + idString + "\\";
          DirectoryInfo imagePathInfo = Directory.CreateDirectory(imageDirectoryPath);
  
          for (int i = 0; i &lt; imageStrings.Count; i++)
          {
              String imageString = imageStrings[i];
              Byte[] binary = Convert.FromBase64String(imageString);
  
              using (FileStream stream = new FileStream(imageDirectoryPath + idString + i.ToString() + ".png", FileMode.Create))
              {
                  using (BinaryWriter writer = new BinaryWriter(stream))
                  {
                      writer.Write(binary);
                  }
              }
          }
      }
  </code></pre>
  
  <p><strong>Edit 2</strong>:  If there is a better to about doing things please let me know.  I am trying to learn here!</p>
  
  <p><strong>Edit 3</strong>:  So upon further examination, I can comment out all this code.  In fact, the second instance of w3wp.exe starts up as soon as a browser hits the website.  I am now wondering if this might have something else to do with our stack?  Its a Flex app, that uses WebOrb for remoting to some C# classes.</p>
  
  <p>Does anyone know why this second open instance of w3wp.exe (owned by NETWORK SERVICE) would prevent the file from opening properly?  Is there some way to get it release it's hold on the file in question?</p>