Ejemplo n.º 1
0
        /// <summary>
        /// Helper, automates the saving of items of virtual list to a CSV files.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SaveVirtualListItemsToCSV(ListView virtualListViewEx1, RetrieveVirtualItemEventHandler handler)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Title            = "Select save file";
                dialog.AddExtension     = true;
                dialog.RestoreDirectory = true;
                dialog.DefaultExt       = "csv";
                dialog.Filter           = "Text file (*.csv)|*.csv";

                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                StringBuilder builder = WinFormsHelper.VirtualListItemsToCSV(virtualListViewEx1, handler);
                using (FileWriterHelper helper = new FileWriterHelper())
                {
                    if (helper.Initialize(dialog.FileName) == false)
                    {
                        MessageBox.Show("Failed to save file.");
                        return;
                    }

                    helper.Write(builder.ToString());
                }
            }
        }
 void FileWriterHelper_DataWrittenEvent(FileWriterHelper helper, StreamWriter writer, string data)
 {
     if (_flushPolicy == FlushPolicyEnum.FlushEachEntry)
     {
         writer.Flush();
     }
     else if (_flushPolicy == FlushPolicyEnum.FlushAutomatic)
     {
         writer.AutoFlush = true;
     }
     else if (_flushPolicy == FlushPolicyEnum.FlushPeriodic)
     {
         if (DateTime.Now - _lastFlush > _periodicFlushInterval)
         {
             _lastFlush = DateTime.Now;
             writer.Flush();
         }
     }
 }
 void FileWriterHelper_DataWrittenEvent(FileWriterHelper helper, StreamWriter writer, string data)
 {
     lock (_syncRoot)
     {
         if (_flushPolicy == FlushPolicyEnum.FlushEachEntry)
         {
             writer.Flush();
         }
         else if (_flushPolicy == FlushPolicyEnum.FlushAutomatic)
         {
             writer.AutoFlush = true;
         }
         else if (_flushPolicy == FlushPolicyEnum.FlushPeriodic)
         {
             if (DateTime.Now - _lastFlush > _periodicFlushInterval)
             {
                 _lastFlush = DateTime.Now;
                 writer.Flush();
             }
         }
     }
 }
        /// <summary>
        /// Helper, automates the saving of items of virtual list to a CSV files.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SaveVirtualListItemsToCSV(ListView virtualListViewEx1, RetrieveVirtualItemEventHandler handler)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Title = "Select save file";
                dialog.AddExtension = true;
                dialog.RestoreDirectory = true;
                dialog.DefaultExt = "csv";
                dialog.Filter = "Text file (*.csv)|*.csv";

                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                StringBuilder builder = WinFormsHelper.VirtualListItemsToCSV(virtualListViewEx1, handler);
                using (FileWriterHelper helper = new FileWriterHelper())
                {
                    if (helper.Initialize(dialog.FileName) == false)
                    {
                        MessageBox.Show("Failed to save file.");
                        return;
                    }

                    helper.Write(builder.ToString());
                }
            }
        }