Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-06
3
Medium Priority
?
857 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 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

916 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