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

解决方案 »

  1.   

    我有上面代码创建一个文件并写入listbox1中,再用indexof取属性,返回正常。分析可能
    是你int Index = ListBox1.Items.IndexOf(TheFile.Name);代码有问题,
    一定是TheFile.Name在listbox中不存在,你在这句加断点,查看一下.Name的值是多少就知道了。
      

  2.   

    谢谢了zhengguoc(爱之海洋) :   我现在知道问题出在哪里了,我用的是listBox1.Items.Add(fi1);   
    不是listBox1.Items.Add(fi1.Name);
    然后用int gf = this.listBox1.Items.IndexOf(fi1.Name); 
    或int gf = this.listBox1.Items.IndexOf(fi1);来接收所以收不到!