【发布时间】:2018-08-01 20:18:30
【问题描述】:
我在文件(目录或任何类型的文件)上使用拖放 ListView。 当我从桌面拖放 My Computer 或 Bin 时发生 NullReferenceException。
我只想跳过这个元素(在日志文本框或其他东西中显示一些文本)。 我不知道如何实现这一目标。我不知道 Desktop 中的 MyComputer 元素是什么类型的对象。
这是我对这个主题逻辑无用的代码:
private void Directories_ListView_DragDrop(object sender, DragEventArgs e)
{
string[] DroppedDirectories = (string[]) e.Data.GetData(DataFormats.FileDrop,false); //array of dd files paths
try
{
foreach (var directory in DroppedDirectories) // here Exception occurs this loop process all dragged and drop files
{
ListViewItem Dir = new ListViewItem(Path.GetFileNameWithoutExtension(directory));
//Place for some checking Logic
if (GetDir.IsDirectory(directory))
{
(.....);// Doing some things
}
else
LogList.Items.Add(DateTime.Now + ": Entry " + Dir.Text + " is not a directory");
}
}
catch(Exception) // it only stops app from crashing,
{
LogList.Items.Add(DateTime.Now + "One of elements was Mycomputer or Bin, process stopped ");
}
}
【问题讨论】:
-
WinForms? WPF? UWP?还有什么? “桌面”标签非常模糊。
-
更正了。无论如何,您的链接与主题无关。
-
您的列表视图是如何配置的?我正在尝试实现您的代码,但我正在使用 AllowDrop = true 找到该事件并处理 DragDrop。当我从桌面拖动回收站时,它会显示不允许的符号并且不会丢弃它。
-
@HandbagCrab 你需要 DragEnter 事件。
-
@TamaraL96 你说你得到一个 NullReferenceException。链接的问题包含有关如何调试这些类型的异常的全面演练,因此是重复的。
标签: c# winforms exception drag-and-drop