Solved

Load and save the content of a listview to binary file.

Posted on 2011-09-06
3
759 Views
Last Modified: 2013-12-17
Hi,

In my previous question I have received a good solution of how
to load and save the content of a listview to binary file.

Only the loading and saving has to go automatic at the startup
and closing of the application.

So i customize the methodes like this:

         private void SaveListview()
         {
             ListViewItem[] items = new ListViewItem[lvNotes.Items.Count];
             lvNotes.Items.CopyTo(items, 0);
             using (System.IO.FileStream fs = System.IO.File.Create(FileName))
             {
              System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
              bf.Serialize(fs, items);
             }
         }

        private void LoadListView()
        {
             ListViewItem[] items = null;
             using (System.IO.FileStream fs = System.IO.File.OpenRead(FileName))
             {
               System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
               items = (ListViewItem[])bf.Deserialize(fs);
                    lvNotes.Items.Clear();
                    lvNotes.Items.AddRange(items);
             }
        }
   
But I get an error : The name 'FileName' does not exist in the current context

Who can help me, i have put the code of my previous question in the code-section.

Greetings, Peter Kiers
private void button2_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                dlg.Filter = "*.bin|*.bin";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ListViewItem[] items = new ListViewItem[listView1.Items.Count];
                    listView1.Items.CopyTo(items, 0);

                    // Serialize the items and save it to a file.
                    using (System.IO.FileStream fs = System.IO.File.Create(dlg.FileName))
                    {
                        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                        bf.Serialize(fs, items);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "*.bin|*.bin";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ListViewItem[] items = null;

                    // Deserialize the items from the binary file.
                    using (System.IO.FileStream fs = System.IO.File.OpenRead(dlg.FileName))
                    {
                        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                        items = (ListViewItem[])bf.Deserialize(fs);
                    }

                    listView1.Items.Clear();
                    // Load the items into the listView1.Items collection.
                    listView1.Items.AddRange(items);
                }
            }
        }

Open in new window

0
Comment
Question by:peterkiers
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36491098
Hello, you need to define the variable FileName in your form class.
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 500 total points
ID: 36491133
May be something like this:
public partial class Form1 : Form
{
    readonly string FileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "MyListViewData.bin");

    public Form1()
    {
        InitializeComponent();
    }

    // ......... etc etc....
}

Open in new window


Also be sure to check if the file exist before to try read from it.
Example:
private void LoadListView()
{
    if (!System.IO.File.Exists(FileName))
        return;

    ListViewItem[] items = null;
    using (System.IO.FileStream fs = System.IO.File.OpenRead(FileName))
    {
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        items = (ListViewItem[])bf.Deserialize(fs);
        lvNotes.Items.Clear();
        lvNotes.Items.AddRange(items);
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 36491191
Now it works. Thansk. Peter Kiers
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question