List<string> lis = new List<string>(); foreach (string str in ofdOpen.FileNames) { li.Add(str); }foreach(string li in lis) { listBox1.Items.Add(str.Substring(li.LastIndexOf('\\')+1,str.Length-str.LastIndexOf('\\')-1)); }
搞一个 list 存 全路径 ,顺序与listbox一样,选哪个项去读哪个相应的路径 倒是能实现你的想法
string str = "c:/123.txt";
string fileName=str.Substring(str.LastIndexOf('/')+1 ));
listBox1.Items.Add(str.Substring(str.LastIndexOf('\\')+1,str.Length-str.LastIndexOf('\\')-1));
foreach(string str in strs)
{
listBox1.Items.Add(str.Substring(str.LastIndexOf('\\')+1,str.Length-str.LastIndexOf('\\')-1));
}
// fileName = "AA"
foreach (string str in ofdOpen.FileNames)
{
li.Add(str);
}foreach(string li in lis)
{
listBox1.Items.Add(str.Substring(li.LastIndexOf('\\')+1,str.Length-str.LastIndexOf('\\')-1));
}
OpenFileDialog openfile = new OpenFileDialog();
openfile.Multiselect = true;
openfile.Filter = "信息Excel2003文件(*.xls)|*.xls";
openfile.Title = "请选择信息";
if (openfile.ShowDialog() == DialogResult.OK)
{
this.TDpath = Path.GetDirectoryName(openfile.FileName);
TDfiles = openfile.SafeFileNames;
}SafeFileNames这个属性啊 取到的就是完整的文件名 去掉前面的路径的 不管你路径多么复杂都可以要装了.Net2.0或者以上版本才有OpenFileDialog.SafeFileNames这个属性