Solved

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

Posted on 2011-09-06
3
717 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now