C# where to place code to auto generate

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 3435 Views - Last Post: 01 December 2010 - 05:38 PM Rate Topic: -----

#1 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

C# where to place code to auto generate

Posted 28 November 2010 - 11:30 AM

hey I have some code that writes data to a csv file, I need it to auto save when an event happens.

So if something pops up in my listbox1 it will write it to the csv?

                {
                    //using (StreamWriter stream = new StreamWriter("C://graph_data.csv"))
                    System.IO.StreamWriter sw = new System.IO.StreamWriter("C://graph_data.csv");

                    foreach (object item in listBox1.Items)

                        sw.WriteLine(item.ToString());

                    sw.Close();
                };
            }

This post has been edited by Sparukus: 28 November 2010 - 11:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C# where to place code to auto generate

#2 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: C# where to place code to auto generate

Posted 28 November 2010 - 11:40 AM

When a form closes, you could do this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //using (StreamWriter stream = new StreamWriter("C://graph_data.csv"));
        System.IO.StreamWriter sw = new System.IO.StreamWriter("C://graph_data.csv");

        foreach (object item in listBox1.Items)

            sw.WriteLine(item.ToString());
 
        sw.Close();

        }



You could do it for any event, but this is just an example.

Hope this helps.

This post has been edited by CreaturGames: 28 November 2010 - 11:42 AM

Was This Post Helpful? 1
  • +
  • -

#3 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# where to place code to auto generate

Posted 28 November 2010 - 11:50 AM

Nah has to be live, I have a filesystem watcher that writes events to the listbox so im looking to have that data in the listbox added to the csv file as soon as its triggered.

Would do it in a console app, but filesystemwatch is kinda strange acting in console.
Was This Post Helpful? 0
  • +
  • -

#4 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: C# where to place code to auto generate

Posted 28 November 2010 - 12:00 PM

View PostSparukus, on 28 November 2010 - 10:50 AM, said:

Nah has to be live, I have a filesystem watcher that writes events to the listbox so im looking to have that data in the listbox added to the csv file as soon as its triggered.

Would do it in a console app, but filesystemwatch is kinda strange acting in console.

I dont get what you mean.

Do you mean that when the file is added to csv, then the listbox will be updated.
Was This Post Helpful? 1
  • +
  • -

#5 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# where to place code to auto generate

Posted 28 November 2010 - 12:20 PM

The filesystem watcher is watching a folder, it updates the listbox1 when a trigger happens (file delete or create) then my code above writes that data in the listbox to a csv file, problem is I have to use a button to write the data id rather just do it by its self i.e on the spot everytime an event happens in my listbox.
Was This Post Helpful? 0
  • +
  • -

#6 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: C# where to place code to auto generate

Posted 28 November 2010 - 12:31 PM

So, you mean you want it to update the listbox when a file is either created or deleted in a specific folder?

Then you want to right a text file showing the files created or destroyed?

In that case, you would do this:

You would create a timer and for each time the timer was up, it would first check if the files exhisted that were in the listbox, then it would repeat the timer when the timer ended.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#7 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# where to place code to auto generate

Posted 28 November 2010 - 12:55 PM

I dont know how to do that? Is there no other way? I mean cant c# just add data from my listbox to my csv file on a continuos basis?
Was This Post Helpful? 0
  • +
  • -

#8 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: C# where to place code to auto generate

Posted 28 November 2010 - 01:12 PM

Hello there,

Presumably you are updating the list box in the gui class (Form class). Therefore, if you want to write things from the list box everytime it is updated, can you not just put the code you posted in a method, and call that method after you update the listbox? Then, everytime the list box is updated, the .csv file will be updated.

This post has been edited by CodingSup3rnatur@l-360: 28 November 2010 - 01:13 PM

Was This Post Helpful? 1
  • +
  • -

#9 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# where to place code to auto generate

Posted 28 November 2010 - 01:21 PM

Thats what I tryed but didnt know how to call it correctly.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Coursework2_windowsapp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            {
                //{
                //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //using (StreamWriter stream = new StreamWriter("C://graph_data.csv"))
                    System.IO.StreamWriter sw = new System.IO.StreamWriter("C://graph_data.csv");

                    foreach (object item in listBox1.Items)

                        sw.WriteLine(item.ToString());

                    //sw.Close();
                };
            }
        }
        private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
        }

        private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
        }

        private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //{
                //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //using (StreamWriter stream = new StreamWriter("C://graph_data.csv"))
                    System.IO.StreamWriter sw = new System.IO.StreamWriter("C://graph_data.csv");

                    foreach (object item in listBox1.Items)

                        sw.WriteLine(item.ToString());

                    sw.Close();
                };
            }
        private void Form1_Resize(object sender, System.EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }
        private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }


        }
        
    }
//}


This post has been edited by Sparukus: 28 November 2010 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: C# where to place code to auto generate

Posted 28 November 2010 - 01:31 PM

You could just call the method every time something is added to the list box i.e. in each of you file system watch event handlers:

private void writeListToFile(){

                              using(System.IO.StreamWriter sw = new System.IO.StreamWriter("C://graph_data.csv")){ 



                              foreach (object item in listBox1.Items)  

   
                              sw.WriteLine(item.ToString());

}  

 private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
            writeListToFile();
        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
            writeListToFile();
            
        }

        private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
            writeListToFile();
        }

        private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
           listBox1.Items.Add(e.ChangeType + ": " + e.Name);
           writeListToFile();
        }





That way, every time you add something to the list box, for whatever reason, the contents of the .csv file get updated. The way you have designed the system, there is no point in creating a custom event that is triggered when the list box is updated, as everything is done in the gui class. Therefore, the gui class would trigger and handle the event, which is slightly pointless in this instance.

This post has been edited by CodingSup3rnatur@l-360: 28 November 2010 - 01:35 PM

Was This Post Helpful? 1
  • +
  • -

#11 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: C# where to place code to auto generate

Posted 28 November 2010 - 01:45 PM

If I may ask, what is the point of this?

It seems irrelevant to make a file watcher, unless there is a proper cause for it.

That is just my opinion. Keep going if you want to.
Was This Post Helpful? 1
  • +
  • -

#12 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# where to place code to auto generate

Posted 28 November 2010 - 04:10 PM

Is there a way to add a date and time stamp to the filesystem watcher events?

e.Name im sure this can be changed but cant find the relative info for the specific task?

Wait its ok got it

DateTime.Now.ToString("yyyy/MM/dd  hh:mm:ss")

This post has been edited by Sparukus: 28 November 2010 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#13 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: C# where to place code to auto generate

Posted 28 November 2010 - 04:36 PM

It seems you are creating your event handlers through Visual Studio, and not through code. You can save yourself some work by handling the Changed, Created, and Deleted with the same event handler. After you created the event handler for one, assign the event handler to the other events. You could also do it through code as follows.

        public Form1()
        {
            InitializeComponent();
            fileSystemWatcher1_Changed += new System.IO.FileSystemEventHandler(fileSystemWatcher1_ChangeMade);
            fileSystemWatcher1_Created += fileSystemWatcher1_ChangeMade;
            fileSystemWatcher1_Deleted += fileSystemWatcher1_ChangeMade;
            {
                //{
                //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //using (StreamWriter stream = new StreamWriter("C://graph_data.csv"))
                    System.IO.StreamWriter sw = new System.IO.StreamWriter("C://graph_data.csv");

                    foreach (object item in listBox1.Items)

                        sw.WriteLine(item.ToString());

                    //sw.Close();
                };
            }
        }
        
        private void fileSystemWatcher1_ChangeMade(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
            // Write out your log file
        }

        private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            listBox1.Items.Add(e.ChangeType + ": " + e.Name);
            // Write out your log file
        }


Was This Post Helpful? 2
  • +
  • -

#14 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# where to place code to auto generate

Posted 29 November 2010 - 08:21 AM

Thanks for all the help guys thats me got it running and sitting in my system tray, was wondering tho how can I do this in a console app? Would it be possible for a quick convert?
Was This Post Helpful? 0
  • +
  • -

#15 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: C# where to place code to auto generate

Posted 29 November 2010 - 08:45 AM

If you're using a Console you cannot use anything in the Forms namespace.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2