0
votes

Ok, in c# I have tried a number of ways to get a listbox to take the contents of a textfile that is populated by other programs, to list the contents. And if certain conditions are true executes a program based off the contents of the first item in the listbox from the textfile. After the program runs its course it deletes the line of execution from the text file, (and this is where my program doesnt continue) and I want the listbox to refresh or reload the text file, once again loading a program based off the contents of the text files first line.

Now if using a listbox isnt the way to go, I am all ears. I just want to be able to display what is in queue. The rest of my program runs great, just this portion, I can't seem to figure out. I have tried to use FileWatcher, and also bindsource, but perhaps I am doing something wrong. I am very new to c#, so any help is appreciated. If need be I can take snippets of my code and display on here. Thanks.

EDIT: Here is some of my current code. It is very butchered as of current as I have been trying different approaches. I dont have it setup to load anything yet, still trying to get it to update when the textfile changes.

public partial class Form1 : Form
{


    private FileSystemWatcher _watcher;
   List<string> myList =  new List<string>();

    public Form1()
    {
        InitializeComponent();

        myList = System.IO.File .ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();

        BindingSource binding = new BindingSource(myList.ToList(), null);
        GPSCOM.DataSource = binding;
        _watcher = new FileSystemWatcher();
        _watcher.Path = Path.GetDirectoryName("C:\\inetpub\\wwwroot\\imomo");
        _watcher.Filter=Path.GetFileName("C:\\inetpub\\wwwroot\\imomo\\Orders.txt");
        _watcher.SynchronizingObject = GPSCOM;
        _watcher.EnableRaisingEvents = true;
        _watcher.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
        _watcher.Error += new ErrorEventHandler(GPSCOMerror);
        GPSCOM.SelectionMode = SelectionMode.One;
        //myList.FirstOrDefault();
         if (serialPort.IsOpen)
        {
            return;
        }
        else
        {
            for (; ; )
            {
                switch ((string)GPSCOM.SelectedItem)
                {
                    case "task1":

                        var lines = System.IO.File.ReadAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt");
                        System.IO.File.WriteAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt", lines.Skip(1).ToArray());
                        //BindingSource binding = new BindingSource(myList.ToList(), null);

                        myList = System.IO.File.ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();
                        GPSCOM.DataSource = binding;
                        GPSCOM.SelectionMode = SelectionMode.One;
                        myList.FirstOrDefault();
                        GPSCOM.SetSelected(0, true);
                        GPSCOM.Refresh();

                        return;
                    case "task2":

                        var lines1 = System.IO.File.ReadAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt");
                        System.IO.File.WriteAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt", lines1.Skip(1).ToArray());
                        //BindingSource binding = new BindingSource(myList.ToList(), null);

                        myList = System.IO.File.ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();
                        GPSCOM.DataSource = binding;
                        GPSCOM.SelectionMode = SelectionMode.One;
                        myList.FirstOrDefault();
                        GPSCOM.SetSelected(0, true);
                        GPSCOM.Refresh();

                        return;
                    case "Task3":

                        var lines2 = System.IO.File.ReadAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt");
                        System.IO.File.WriteAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt", lines2.Skip(1).ToArray());
                        //BindingSource binding = new BindingSource(myList.ToList(), null);

                        myList = System.IO.File.ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();
                        GPSCOM.DataSource = binding;
                        GPSCOM.SelectionMode = SelectionMode.One;
                        myList.FirstOrDefault();
                        GPSCOM.SetSelected(0, true);
                        GPSCOM.Refresh();

                        return;
                    case "":
                        return;
                }
            }
            }

    }

private void GPSCOM_SelectedIndexChanged(object sender, EventArgs e) {

        BindingSource binding = new BindingSource(myList.ToList(), null);

        myList = System.IO.File.ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();
      GPSCOM.DataSource = binding;
        GPSCOM.Refresh(); 
    }        
    private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
    {

        BindingSource binding = new BindingSource(myList.ToList(), null);

        myList = System.IO.File.ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();
       GPSCOM.DataSource = binding;
        GPSCOM.Refresh();


         }
    }
1
Please provide us some code, it's very difficult otherwise.Romano Zumbé
If you're using Process.Start to execute a program, then you could listen to the exit event or wait until execution has finished, then just remove the first entry from the listbox. No need to watch the file or do any binding..Trent

1 Answers

0
votes

You need something like this?

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.DataSource = TaskQue.GetTasks();
        }

        private void Execute(object sender, EventArgs e)
        {
            string task = TaskQue.Pop();
            //execute task;
            listBox1.DataSource = TaskQue.GetTasks();
        }

        private void AddTask(object sender, EventArgs e)
        {
            TaskQue.Push(textBox1.Text);
            listBox1.DataSource = TaskQue.GetTasks();

        }
    }

    public class TaskQue
    {
        public static string txtPath = "C:/a.txt";

        public static string Pop()
        {
            StreamReader sr = new StreamReader(txtPath);
            string result = sr.ReadLine();
            string remaining = sr.ReadToEnd();
            sr.Close();
            StreamWriter sw = new StreamWriter(txtPath, false);
            sw.Write(remaining);
            sw.Close();
            return result;
        }

        public static void Push(string s)
        {

            StreamWriter sw = new StreamWriter(txtPath, true);
            sw.WriteLine(s);
            sw.Close();
        }

        public static IEnumerable<string> GetTasks()
        {
            return new List<string>(File.ReadLines(txtPath));
        }
    }