Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have done a drag and drop from a text file located in the desktop into a list box no problem.
but when I place the text file into any folder and create his shortcut located in my desktop, and D&D into my listbox I got a "L" showing in the listbox.
can anyone give me an example?
Super Thanks
Posted
Comments
BillWoodruff 31-Dec-14 5:16am    
I think Ravi's answer here will probably meet your needs; in the past I found the information and code here:

https://astoundingprogramming.wordpress.com/2012/12/17/how-to-get-the-target-of-a-windows-shortcut-c/

Very useful: shows how to detect different types of short-cuts: windows-installer short-cuts (msi), etc.

Use the Windows Scripting Host to determine a shortcut file's target.  See this article for IWshRuntimeLibrary wrapper: Creating Shell Links (Shortcuts) in .NET Programs Using WSH[^].  For example:
C#
if (Path.GetExtension(droppedFilename).ToLowerInvariant() == ".lnk") {
    try {
       IWshRuntimeLibrary.WshShell wsh new IWshRuntimeLibrary.WshShell();
       IWshRuntimeLibrary.IWshShortcut shortcut =
         (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut (droppedFilename);
       string target = shortcut.TargetPath;  // Add target to your ListBox
    }
    catch {
        // Error resolving shortcut
    }
}
/ravi
 
Share this answer
 
Comments
BillWoodruff 31-Dec-14 5:16am    
+5
Ravi Bhavnani 31-Dec-14 9:54am    
Gracias, mon ami. And a very happy New Year!

/ravi
/* Super Ravi! here is a small sample of drag and drop example of a text file OR shortcut file to be dropped in listbox1 from any folder.
shears!.
*/
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IWshRuntimeLibrary;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /*
         * this project allows you to drop a text file OR a shortcut into in listbox1
         * 
         add 2 listbox 1,2 then under dragdrop and dragenter event of listbox1 cut past code below
         * you need to add a reference to the Windows Script Host component: Project...Add Reference...COM tab...Windows Script Host Object.
         * This will create the interop assembly and provide you with the IWshRuntimeLibrary interface.
        enjoy Erol.
        */
        private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs de)
        {
            try
            {
                if (de.Data.GetDataPresent(DataFormats.Text) ||
                   de.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    de.Effect = DragDropEffects.Copy;
                }
                else
                {
                    de.Effect = DragDropEffects.None;
                }
            }

            catch (System.Exception excep)
            { MessageBox.Show(excep.Message); }
        }

        public static string ShortCutRunMethod;
        public static string[] lines2;
        private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs de)
        {

            try
            {
                listBox1.Items.Clear();
                listBox1.AllowDrop = true;

                string fileName = "";

                if (de.Data.GetDataPresent(DataFormats.Text))
                {
                    fileName = (string)de.Data.GetData(DataFormats.Text);

                }
                else if (de.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string[] fileNames;
                    fileNames = (string[])de.Data.GetData(DataFormats.FileDrop);
                    fileName = fileNames[0];
                    listBox2.Items.Add("Filename:" + fileName);
                }

                //string  DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

                if (Path.GetExtension(fileName).ToLowerInvariant() == ".lnk")
                {
                    try
                    {
                        IWshRuntimeLibrary.WshShell wsh = new IWshRuntimeLibrary.WshShell();
                        IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(fileName);

                        string target = shortcut.TargetPath;  // Add target to your ListBox
                        listBox2.Items.Add("targetPath: " + target);

                        ShortCutRunMethod = target.Replace(".lnk", "");
                        listBox2.Items.Add("ICON:   " + target);
                        lines2 = System.IO.File.ReadAllLines(ShortCutRunMethod);
                        goto skipthis;
                    }

                    catch (System.Exception excep)
                    { MessageBox.Show(excep.Message); }

                }

                lines2 = System.IO.File.ReadAllLines(fileName);

            skipthis:

                foreach (string line in lines2)
                {
                    listBox1.Items.Add(line);
                }

            }
            catch (System.Exception excep)
            { MessageBox.Show(excep.Message); }
        }

    }
}
 
Share this answer
 
Comments
BillWoodruff 3-Jan-15 22:37pm    
+5 glad to see you got this working; that's good code to have on hand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900