string path = @"e:\MyTest.txt";
FileInfo fi1 = new FileInfo(path); if (!fi1.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi1.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
listBox1.Items.Add(fi1.Name);
int gf = this.listBox1.Items.IndexOf(fi1.Name);
FileInfo fi1 = new FileInfo(path); if (!fi1.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi1.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
listBox1.Items.Add(fi1.Name);
int gf = this.listBox1.Items.IndexOf(fi1.Name);
是你int Index = ListBox1.Items.IndexOf(TheFile.Name);代码有问题,
一定是TheFile.Name在listbox中不存在,你在这句加断点,查看一下.Name的值是多少就知道了。
不是listBox1.Items.Add(fi1.Name);
然后用int gf = this.listBox1.Items.IndexOf(fi1.Name);
或int gf = this.listBox1.Items.IndexOf(fi1);来接收所以收不到!