c#如何检测并处理DragDrop事件上的.URL文件类型 [英] c# How to detect and process a .URL file type on DragDrop event

查看:174
本文介绍了c#如何检测并处理DragDrop事件上的.URL文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了不少研究,但到目前为止还没有水果...



我发现的唯一链接有点类似于我的情况,甚至可以工作:






  • 以下是当前代码我正在文本框的DragDrop事件处理程序中使用,似乎不起作用:

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

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



    (不知道这会有什么区别,但是我试着替换 DataFormats.FileDrop DataFormats.Serialization 以及没有后果效果的DataFormats.Html )



    另外,由于一些奇怪的原因,程序在上面的代码块的第一行立即中断。 (它也恰好是我的DragDrop事件中的第一行)



    我的问题是:如何更正或修改代码在任何程度上,即使完全改变代码),以便它可以识别 .url 扩展名的互联网快捷方式文件,然后继续像普通文件那样对待它。



    ,为什么在执行第一行后立即停止代码的执行,即使已经为几行设置了断点第一行之后(如完全跳过if语句)?



    如果不是太多,怎么只让下面这行:

      e.Effect = DragDropEffects.Link; 

    当文件只有互联网快捷方式类型 .url ,在DragEnter事件处理程序中,或者是不可能的?



    我知道Notepad ++能够打开和读取内容,甚至编辑互联网快捷方式文件。虽然如果我可以得到代码来识别快捷方式,我想我可以像普通的文件一样读取。



    我喜欢一个工作的答案代码,如果可能的话。对于这个长期的问题,我想尽可能的尽量地解决我的问题。

    解决方案

    更新: @ taffer的答案解释了为什么你的代码行为奇怪。



    你可以使用下面的代码,我已经测试了;项目的行为如您所述。

      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
    }

    }

    在第二个问题的旁注:可能是抛出异常,使用try catch来验证。


    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:

    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.

    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
    }
    

    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.

    (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.)

    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)

    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.

    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;
    

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

    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.

    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.

    解决方案

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

    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.

    这篇关于c#如何检测并处理DragDrop事件上的.URL文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆