Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:54:53+00:00 2026-06-12T04:54:53+00:00

I am using a memorystream object to write values from streamwriter object into memory.

  • 0

I am using a memorystream object to write values from streamwriter object into memory. I have several methods which are for logging errors and values so I pass my memorystream object between them as a parameter.

My problem is the memorystream doesn’t contain values. See code below:

    #region csvlogging
    public static void initiateCsvLogging(SortedList<int, string> logList, SortedList<int, string> errorList) // Handles csv upload logging
    {
        logMsgError(errorList, logList);
    }

    public static void logMsgError(SortedList<int, string> errorList, SortedList<int, string> logList)
    {
        using (MemoryStream ms = new MemoryStream())
        {
           StreamWriter sw = new StreamWriter(ms);
            try
            {
                sw.WriteLine("Errors:");
                foreach (KeyValuePair<int, string> k in errorList)
                {

                    if (k.Value == null)
                    {
                        sw.WriteLine("No errors reported.");
                    }
                    else
                    {
                        sw.WriteLine(k.Key + "," + k.Value);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                sw.WriteLine("");
            }

            logMsg(logList, ms);

        }
    } // Handles data upload error logging for csv

    public static MemoryStream logMsg(SortedList<int, string> logList, MemoryStream ms)
    {
        string DateNow = System.DateTime.Now.Date.ToString("yyyy-MM-dd");
        string FileName = "log " + DateNow + ".csv";
        char[] delimiterChars = { ',' };

        try
        {
            // Write values to textfile and save to Log folder
            using (StreamWriter sw = new StreamWriter(ms))
            {
                if (logList.Keys.Count == 0)
                {
                    sw.WriteLine("No new users added to Active Directory.");
                }
                else  // If keys > 0 then new users have been added to AD
                {
                    sw.WriteLine("Username" + "," + "Password" + "," + "Company" + "," + "Email");
                    foreach (KeyValuePair<int, string> k in logList)
                    {
                        string[] values = k.Value.Split(delimiterChars);
                        sw.WriteLine(values[0] + "," + values[1] + "," + values[2] + "," + values[3]);
                    }
                }

                try
                {
                    sw.Flush();
                    sendLogByEmail(new MemoryStream(ms.ToArray()), FileName);
                }
                catch (Exception ex)
                {

                }
            }
        }

        catch (ThreadAbortException ex)
        {

        }

        finally
        {

        }
        return ms;
    } // Handles logging for csv

    public static void sendLogByEmail(MemoryStream ms, string error, int count)
    {
        //Send mail by attachment code
        SmtpClient smtpclient = new SmtpClient();
        //smtpclient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
        //smtpclient.UseDefaultCredentials = true;
        smtpclient.Host = "ldnmail";


        MailMessage message = new MailMessage("nick.gowdy@orcinternational.co.uk", "nick.gowdy@orcinternational.co.uk");
        System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
        System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
        attach.ContentDisposition.FileName = error;

        message.Attachments.Add(attach);
        smtpclient.Send(message);

    }
    #endregion

Because I am not using the USING keyword do I have to write some more code for the memorystream object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:54:54+00:00Added an answer on June 12, 2026 at 4:54 am

    You have to flush (Close) the StreamWriter.

            finally
            {
                sw.WriteLine("");
            }
    
            sw.Flush();             
    
            logMsg(logList, ms);
    

    But in the end you use all data to send it by mail:

       System.Net.Mime.ContentType ct = new
             System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
       System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
    

    It would seem a lot easier to collect the information in a StringBuilder through an attached StringWriter.

    There are issues with a StreamWriter closing its Stream, that may be part of your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this method to write to a MemoryStream object, which is subsequently stored
I have a big object in memory which I want to save as a
Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter =
Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms
I have a JSON string in a MemoryStream. I am using the following code
I found I could generate XDocument object from html by using SgmlReader.SL. https://bitbucket.org/neuecc/sgmlreader.sl/ The
I have problem with deserialize document to object using XmlSerializer class. Code my function
I have the following code public static byte[] Compress(byte[] CompressMe) { using (MemoryStream ms
Lets say I wanna write a string Hello World into a MemoryStream and read
public static byte[] Compress(byte[] data) { using (MemoryStream ms = new MemoryStream()) { using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.