我对这种方法有点麻烦。combolistbox.Item.Add(FileInfo)不一致?
当我通过dragDropFiles中的FileInfo类型对象循环并将每个对象单独添加到CLB时,我会检查该项目时获取FullName属性(文件的完整路径,这是我需要的)。 但是,使用hotFolderFiles而不是路径,它只给出了文件名。
我不明白这一点,因为他们以相同的方式添加相同的对象类型。
(我也试过让FileInfo的使用DirectoryInfo的,而不是我的字典用同样的结果热文件夹中的文件)
为什么这种行为不一致? (我怎样才能得到它返回的fileInfo全名,而不是名称?)
public frmFilesFound(string hotFolderPath, Dictionary<string, FileInfo> dragDropFiles, Dictionary<string, FileInfo> hotFolderFiles, bool ReadOnly)
{
try
{
InitializeComponent();
readOnly = ReadOnly;
btnSelectAll.Visible = true;
clbSelectFilesFound.Visible = true;
clbSelectFilesFound.FormattingEnabled = true;
clbSelectFilesFound.Format += (s, e) => { e.Value = string.Format("{0}", ((FileInfo)e.ListItem).Name); };
foreach (FileInfo fileInfo in dragDropFiles.Values)
{
if (!clbSelectFilesFound.Items.Contains(fileInfo))
{
try
{
// If file not already present, add it to listbox
clbSelectFilesFound.Items.Add(fileInfo);
}
catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); }
}
}
//intended to be hot folder path
if (!String.IsNullOrEmpty(hotFolderPath))
{
DirectoryInfo dirInfo = new DirectoryInfo(hotFolderPath);
foreach (FileInfo fileInfo in dirInfo.GetFiles())
//foreach (FileInfo fileInfo in hotFolderFiles.Values)
{
if (!clbSelectFilesFound.Items.Contains(fileInfo))
{
try
{
clbSelectFilesFound.Items.Add(fileInfo);
}
catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); }
}
}
}
lblDisplayedSelectMessage.Text = "More than one file is waiting. Please select the files you would like to use.";
}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
}
这是一个组合框的只是美女,你可以将对象添加到它。否则,f.e.属性SelectedItem将没有用处。 – 2012-04-02 12:52:20
然后将其数据成员过滤器设置为“FullName”,或者使用fileInfo自身标记该项目,以备将来参考。 – SimpleVar 2012-04-02 12:54:35
我正在添加对象,因为我想在comboListBox中显示文件名,但是我想从选择结果中使用FullName。 – negligible 2012-04-02 13:10:34