Search
Close this search box.

Save all your lists items into a file in C#

If you are using C#, you can do the following: I needed to take all my log output that I sent to a list and save it to a file for later. This is a method that can be used to put some log information to a file. Or, can be used to store data you will need later to debug your program.

try

{
  int i = Read_Memory_Add_List.Items.Count;

  object[] obj = new object[i];

  Read_Memory_Add_List.Items.CopyTo(obj, 0);

  i = obj.Length;

  // use ,true if you want to append data to file

  // this process will open a save file dialog and give the option to choose

  // file location, name, and ext.  then when you press save it will save it

  FileDialog oDialog = new SaveFileDialog();

  oDialog.DefaultExt = "log";

  oDialog.FileName = "Memory_Add_List_SavetoFile";

  if (oDialog.ShowDialog() == DialogResult.OK)

  {
    using (System.IO.StreamWriter file =
               new System.IO.StreamWriter(@oDialog.FileName))

        foreach (string line in obj)

    {
      file.WriteLine(line);
    }
  }

}

catch (Exception exp)

{
  MessageBox.Show("" + exp);
}

===========================================================

In the above code example, Read_Memory_Add_List is the list that contains all your data log

I used FileDialog  to be able to save the data to a file.  I am overwriting the file if it exists.

You can also use this to fill your list

Reg_IO_Read_SINGLE_REG_List.Items.Add(

In this list example, I used some of the Hexdec conversion and reading to be able to gather all my data in the list items.  I will be adding more posts to show how you can easily convert from number system to another.

DateTime.Now.ToShortDateString() + " "+ DateTime.Now.ToLongTimeString() + " Bank: " + (Convert.ToUInt32(Read_Bank_No_text.Text) + j).ToString() + " Reg: " + (Convert.ToUInt32(Read_Reg_No_Text.Text) + j).ToString() + " Value: 0x" + DTB_Utilities.Indian_Convert(command.u32_aData[i]).ToString("X8"));
Print posted @ Monday, February 09,
    2009 11 : 33 AM

        Feedback No comments posted yet.

    Post a comment Title : re : Save all your lists items into a file in C #

                                Name :

    Email : Not Displayed

                Website :

    Comment :

    Enter the code shown above :

    News Recent Comments Well,
    XP is the release of new MS Windows;
it came... by Hesham Elsaghir What's an XP? by Joe Article Categories
    C #Linux Exploration Embedded world Archives May,
    2009(2)March, 2009(3)February,
    2009(8)Post Categories C
    #Linux Exploration Embedded world MS Windows Microchip PICs Syndication
    : RSS ATOM Hosted by Subtext Blog
This article is part of the GWB Archives. Original Author: Hesham Elsaghir

Related Posts