title: write and read from byte stream
body: <p>I have a page where the User can either upload their own csv or enter values into a listbox which then creates a csv (in the background).  Regardless of which way the csv gets created I need to upload that csv to our server via a byte stream.  </p>
  
  <p>My problem is that when Im creating the csv I shouldn't have to create a temporary file, I should be able to write to the stream then read it back for uploading.  How can I remove the need for the temporary file?</p>
  
  <p>current code which works (but uses temp file):</p>
  
  <pre><code>try {
                  string filename = DateTime.Now.ToString("MMddyyHmssf");
                  filename = filename + ".csv";
                  string directory = ConfigurationManager.AppSettings["TempDirectory"].ToString();
                  path = Path.Combine(directory, filename);
                  using (StreamWriter sw = File.CreateText(path)) {
  
                      foreach (ListItem item in this.lstAddEmailAddress.Items) {
                          sw.WriteLine(" , ," + item.ToString());
                      }
                  }
              } catch (Exception ex) {
                  string error = "Cannot create temp csv file used for importing users by email address.  Filepath: " + path + ".  FileException: " + ex.ToString();
                  this.writeToLogs(error, 1338);
              }
          }
          // put here for testing the byte array being sent vs ready byte[] byteArray = System.IO.File.ReadAllBytes(path);
          myCsvFileStream = File.OpenRead(path);
          nFileLen = (int)myCsvFileStream.Length;
  </code></pre>
  
  <p>I have tried</p>
  
  <pre><code>Stream myCsvFileStream;
  using (StreamWriter sw = new StreamWriter(myCsvFileStream)) {
  
                      foreach (ListItem item in this.lstAddEmailAddress.Items) {
                          sw.WriteLine(" , ," + item.ToString());
  
                      }
                  }
  </code></pre>
  
  <p>However since myCsvFileStream is not initialized (because stream is a static class) it is always null.</p>
  
  <p>Here is what I do with the data (byte stream) after creating the csv.</p>
  
  <pre><code>byte[] file = new byte[nFileLen];
              myCsvFileStream.Read(file, 0, nFileLen);
              bool response = this.repositoryService.SaveUsers(this.SelectedAccount.Id, file, this.authenticatedUser.SessionToken.SessionId);
              myCsvFileStream.Close();
  </code></pre>
  
  <p>In the end I used <code>StringBuilder</code> to create my csv file contents.  Then got a byte array of its contents and used that to populate my shared stream (I say shared because when the user enters their own CSV file it is a <code>HttpPostedFile</code> but when sending it to our server via the rest call (<code>respositoryservices.saveusers</code>) it uses the same byte stream that it would via this method)</p>
  
  <pre><code>StringBuilder csvFileString = new StringBuilder();
  
              sharedStreamForBatchImport = new MemoryStream();
              foreach (ListItem item in this.lstAddEmailAddress.Items) {
                  csvFileString.Append(",," + item.ToString() + "\\r\\n");
              }
  
              //get byte array of the string
              byteArrayToBeSent = Encoding.ASCII.GetBytes(csvFileString.ToString());
              //set length for read
              byteArraySize = (int)csvFileString.Length;
              //read bytes into the sharedStreamForBatchImport (byte array)
              sharedStreamForBatchImport.Read(byteArrayToBeSent, 0, byteArraySize);
  </code></pre>