获取将数据从资源管理器拖放到Windows窗体的文件名



我正在努力完成一项我以为会很简单的任务。

我有一个Windows窗体应用程序,写在c++/CLI。我希望用户能够将文件拖放到表单中进行某种处理。

我已经将表单行为设置为AllowDropDragEnter事件引发。

如何检索文件名?我在c#中见过这个例子,但没有成功移植到c++/CLI

这是我尝试过的:

private: System::Void tabSingle_DragEnter(System::Object^  sender, 
System::Windows::Forms::DragEventArgs^  e)
{
   if ( e->Effect == DragDropEffects::Link)
   {
      // This is true
   }
   try
   {
      String^ filename = (String^) e->Data->GetData("FileName");
   }
   catch(...)
   {
      // System.InvalidCastException: Impossible to convert an object of type 'System.String[]' in to'System.String'
   }
}

提前感谢!

EDIT:我用收到的异常的描述更新了答案。看起来期望的对象是System.String[]。我应该如何修改这一行,使其具有与c#中的这一行相同的行为(从这里)

Array data=((IDataObject)e.Data).GetData("FileName") as Array;

c++/CLI中的数组语法为array<String^>^ filenames = ...

因为异常告诉你你有一个字符串数组,直接转换为该类型,而不是通用类型Array,就像他们在c#示例中所做的那样。(当然,在使用它之前要检查它是一个字符串数组,等等)

最新更新