C#中如何检测和处理上的DragDrop事件.URL文件类型文件类型、事件、DragDrop、URL

2023-09-07 16:06:43 作者:沉醉不知归路

我已经做了不少研究,但没有水果迄今为止...

I have done quite a few research, but there were no fruits so far...

这是我发现的唯一联系是有点类似于我的情况甚至不工作或者:

The only links that I found are somewhat similar to my situation don't even work either:

简单的拖放例如 How我可以告诉从文件的快捷方式在C#拖放操作? 拖放文件用C# 拖放文件到文本框 e.Data.GetData$p$psent不工作的WinForms拖和滴处理? Simple drag and drop example How can I tell a shortcut from a file in a C# drag and drop operation? Drag and Drop Files with C# drag and drop file into textbox e.Data.GetDataPresent not working in WinForms drag-and-drop handler?

基本,首先,我要的code到能够识别该文件是一个的的.url 的延伸,或者说, Internet快捷方式,最典型的是受聘于谷歌Chrome,以及Mozilla Firefox浏览器,以及最有可能的,所有其他网络浏览器,我没有进行广泛的测试。

Basically, first of all, I want the code to be able to recognize that the file is of a ".url" extension, or rather, an internet shortcut, most typically employed by Google Chrome, as well as Mozilla Firefox, and most likely, all other web browsers which I did not extensively test.

如果你不知道什么类型的快捷方式,我指的是,我的意思是像下面的图片。

In case you do not know what type of shortcut I am referring to, I mean something like in the picture below.

下面是我使用的一个文本框的DragDrop事件处理程序当前code,它似乎不工作:

Below is the current code that I am using in the DragDrop event handler of a textbox, and it doesn't seem to work:

string file = (string)(e.Data.GetData(DataFormats.FileDrop, false));
if (Path.GetExtension(file) == ".url")
{
     //Do Stuff Here
}

显然,code甚至不能得到快捷的所谓文件名。此快捷方式是已经被拖放从浏览器在桌面上的那种。因此,在这种情况下,该快捷方式一个被拖放为code键处理

Apparently, the code can't even get the supposed file name of the shortcut. This shortcut is the kind that has already been dragged and dropped to the desktop from a browser. Thus, in this case, that shortcut is the one being dragged and dropped for the code to process.

(不知道这是否会令任何区别,但我想代以 DataFormats.FileDrop 的有的 DataFormats.Serialization 的和的 DataFormats .HTML 的,没有高建国的影响。)

(Not sure if this will make any difference, but I tried substituting "DataFormats.FileDrop" with "DataFormats.Serialization" as well as "DataFormats.Html" with no postitive effects.)

此外,对于一些奇怪的原因,该程序会立即以高于code块的第一行中断。 (这也恰好是第一行中我DragDrop事件)

Also, for some weird reason, the program immediately breaks off at the very first line of the code block above. (It also happens to be the very first line in my DragDrop event)

我的问题是:如何能在code进行修正或修改(在任何程度上,即使完全改变了code),因此它可以识别的Internet快捷方式文件的.url 的扩展,然后继续把它像一个正常的文件。

My question is: How can the code be corrected or modified (to any extent, even if completely changing the code) so that it can recognize the internet shortcut file of .url extension, then proceed to treat it like a normal file.

同时,为什么是$ C $的执行的C立即停止执行的第一线,即使断点​​已经设置了第一行之后的几行(如跳过之后if语句完全)?

Also, why is the execution of the code immediately stopping right after executing the first line, even though breakpoints have been set for the few lines after the first line (as in skipping the if statement completely)?

而如果它不是太多,如何才让下面的一行:

And if it is not too much, how to only let the line below:

e.Effect = DragDropEffects.Link;

工作时,该文件仅是Internet快捷方式的类型,的.url 的,在DragEnter事件处理程序,或者是不可能的?

work when the file is only of the internet shortcut type, ".url", in the DragEnter event handler, or is that impossible?

我知道,记事本+ +是能够打开并阅读的内容,甚至编辑Internet快捷方式文件。虽然,如果我能得到code识别快捷方式,我想那我就可以从中读取像一个正常的文件。

I know that Notepad++ is able to open and read the contents and even edit the internet shortcut files. Although if I can get the code to recognize the shortcut, I think I will then be able to read from it like a normal file.

我会preFER有工作code,如果可能的答案。很抱歉的长期问题,因为我想尽量为precise尽可能与我的问题。

I would prefer an answer with working code, if possible. Sorry for the long question, as I wanted to try to be as precise as possible with my question.

推荐答案

更​​新: @ taffer的回答解释了为什么你的$ C $Ç行为古怪。

Update: @taffer's answer explains why your code behaves odd.

您可以用code以下,我已经测试它;项目表现为你描述。

You can use the code below, I have tested it; Project behaves as you described.

    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
        string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
        {
            if (Path.GetExtension(file[0]) == ".url")
            {
                //Do Stuff Here
                //
            }
        }
    }

    private void textBox1_DragEnter(object sender, DragEventArgs e)
    {
        string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);

        if (Path.GetExtension(file[0]) == ".url")
        {
            e.Effect = DragDropEffects.Link;
            //Do Stuff Here
        }

    }

在一个侧面说明了第二个问题:可能引发异常,使用尝试捕捉验证

On a side note for second question: Probably an exception being thrown, use try catch to verify.