Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-06
3
Medium Priority
?
834 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 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