Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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