Reputation: 2403
I've attempted (using dozens of examples) to create a simple WinForms app that will allow me to drag-and-drop a simple text file into an input box on a custom DotNet program. Unfortunately, there appears to be no examples that actually work in Windows 7.
Here's a simple example (from another post that's been referenced all over the place) but it does not work at all.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DragAndDropTestCSharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.AllowDrop = true;
this.DragEnter += Form1_DragEnter;
this.DragDrop += Form1_DragDrop;
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
foreach (string fileLoc in filePaths)
{
// Code to read the contents of the text file
if (File.Exists(fileLoc))
{
using (TextReader tr = new StreamReader(fileLoc))
{
MessageBox.Show(tr.ReadToEnd());
}
}
}
}
}
}
}
Could this be a UAC issue? If so, why does every other app in the world seem to do this simple, yet elusive feat of drag and drop with UAC on?
Does someone have a real, working example of getting this to work in Windows 7?
Upvotes: 0
Views: 2051
Reputation: 36
I've tried your sample and it works fine. Check if you have hook the Load event to the form object to the Form1_Load handler and your namespace is the same.
this.Load += new System.EventHandler(this.Form1_Load);
or via properties editor:
Upvotes: 1
Reputation: 2403
Ok, I discovered that running VS under Administrator (which I had to do for another project) was the culprit. Loading VS in normal user mode, it works fine with UAC on. Thanks all for your input!
Upvotes: 1