好的,在 c# 中,我尝试了多种方法来获取列表框,以获取由其他程序填充的文本文件的内容,以列出内容。如果某些条件为真,则根据文本文件中列表框中第一项的内容执行程序。在程序运行它的过程之后,它会从文本文件中删除执行行,(这是我的程序不会继续的地方)并且我希望列表框刷新或重新加载文本文件,再次加载基于内容的程序文本文件的第一行。
现在,如果使用列表框不是要走的路,我全神贯注。我只想能够显示队列中的内容。我的程序的其余部分运行良好,只是这部分,我似乎无法弄清楚。我曾尝试使用 FileWatcher 和 bindsource,但也许我做错了什么。我对 c# 很陌生,因此感谢您的帮助。如果需要,我可以截取我的代码片段并在此处显示。谢谢。
编辑:这是我当前的一些代码。到目前为止,由于我一直在尝试不同的方法,因此它非常被屠杀。我还没有设置它来加载任何东西,仍然试图在文本文件更改时更新它。
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;
}
}
}
}
私人无效GPSCOM_SelectedIndexChanged(对象发送者,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();
}
}