/*
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!.
*/
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();
}
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);
}
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;
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); }
}
}
}