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();
}
}