Drag&drop from explorer delete my source file - c#

I work on a Outlook plugin, and i want to drop a file in a listbox (in a outlook region) from explorer. I simply do this:
private void InitializeComponent()
{
this._shareList.DragDrop += new System.Windows.Forms.DragEventHandler(this._shareList_DragDrop);
this._shareList.DragEnter += new System.Windows.Forms.DragEventHandler(this._shareList_DragEnter);
}
private void _shareList_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
e.Effect = DragDropEffects.Link;
Cursor.Current = Cursors.Arrow;
}
}
private void _shareList_DragDrop(object sender, DragEventArgs e)
{
string[] tab = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string filePath in tab)
AttachFile(new FileInfo(filePath));
}
When i drop file from the explorer, the source file is deleted of the disk! Why? Outlook catch the drop event?
So how can i prevent that?
Thanks for your help.

OK I do an error in my code :(
I delete unfortunately the file...
Sorry to bother you.

Related

Change the selection in the ListBox

I add audio files to the ListBox by clicking on the Button. Then OpenFileDialog (the code below) works. I use the added files ListBox to play (the code below). I click on button_play. There is a problem. When I click on the Button for sorting using listBox1.Sorted = true. After sorting, the audio file that should be played is not played. The audio file that was in this place before sorting is played. That is, if all songs had a number, then after sorting only the names were changed, but the numbers have not changed. And when you click button_play is played by the number.
private void button_add_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
label_load.Text = list_catalog.Items.Count.ToString();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
foreach (String file in openFileDialog1.FileNames)
{
if (list_catalog.Items.Contains(Vars.GetFileName(file)))
{
}
else
{
Vars.Files.Add(file);
list_catalog.Items.Add(Vars.GetFileName(file));
hello.Visible = false;
}
}
}
private void button_play_Click(object sender, EventArgs e)
{
string current = Vars.Files[list_catalog.SelectedIndex];
Vars.CurrentTrackNumber = list_catalog.SelectedIndex;
BassLike.Play(current, BassLike.Volume);
label_time1.Text = TimeSpan.FromSeconds(BassLike.GetPosOfStream(BassLike.Stream)).ToString();
label_time2.Text = TimeSpan.FromSeconds(BassLike.GetTimeOfStream(BassLike.Stream)).ToString();
xrewind.Maximum = BassLike.GetTimeOfStream(BassLike.Stream);
xrewind.Value = BassLike.GetPosOfStream(BassLike.Stream);
timer1.Enabled = true;
}
I think this will work...
string current = list_catalog.SelectedItem as string;
Vars.CurrentTrackNumber = Vars.Files.IndexOf(current);

Drag and Drop forbidden on panel even though AllowDrop is set to true [duplicate]

This question already has an answer here:
File drag and drop not working on listbox
(1 answer)
Closed 7 years ago.
I have a panel on my form on which I have allowed drag and drop. I have written the code to DragEnter and DragDrop events and they were working fine last time I checked. But now when I drag a file over my panel I get the forbidden cursor and the events are no longer triggered. I have looked in my entire project to see if I am disabling AllowDrop somewhere, but I don't.
Here are my events together with the functions they are performing:
this.pnlNoPostbagFolder.AllowDrop = true;
this.pnlNoPostbagFolder.DragDrop += new System.Windows.Forms.DragEventHandler(this.pnlNoPostbagFolder_DragDrop);
this.pnlNoPostbagFolder.DragEnter += new System.Windows.Forms.DragEventHandler(this.pnlNoPostbagFolder_DragEnter);
private void pnlNoPostbagFolder_DragDrop(object sender, DragEventArgs e)
{
FileListDragDrop(sender, e);
}
private void pnlNoPostbagFolder_DragEnter(object sender, DragEventArgs e)
{
FileListDragEnter(sender, e);
}
private void FileListDragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void FileListDragDrop(object sender, DragEventArgs e)
{
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
for (int i = 0; i < s.Length; i++)
{
if (Path.GetExtension(s[i]).Equals(".csv", StringComparison.InvariantCultureIgnoreCase) || Path.GetExtension(s[i]).Equals(".sql", StringComparison.InvariantCultureIgnoreCase))
{
string source = s[i];
string destination = Common.Conf.PostbagFolderLocation + "\\" + Path.GetFileName(s[i]);
if (File.Exists(destination))
{
DialogResult dr = MsgBox.Show(string.Format("A file named '{0}' already exists in the Postbag folder. Overwrite?", Path.GetFileName(s[i])), "File Exists", MsgBox.Buttons.YesNo, MsgBox.Icon.Question);
if (dr == DialogResult.Yes)
{
File.Copy(source, destination, true);
RefreshPostbagFolder();
}
}
File.Copy(source, destination, true);
}
else
MsgBox.Show("File extension not supported", "Add File", MsgBox.Buttons.OK, MsgBox.Icon.Error);
}
}
All of your code seems OK and I don't have any issue with your code in my machine, It seems a UAC issue:
Q: Why Doesn’t Drag-and-Drop work when my Application is Running Elevated?
UAC elevation does not allow drag and drop

Remove icon automatically by dropped file on RichTextBox

I set to true the AllowDrop implemented the DragOver and DragDrop events RichTextBox. On DragDrop event I load the dropped text files' contents on the RTB but it does add the icon of the file in RTB I'd to remove it:
Edit: Here's my code:
void msg_setup_dragDrop()
{
msg_textBox.AllowDrop = true;
msg_textBox.EnableAutoDragDrop = true;
msg_textBox.DragEnter += new DragEventHandler(msg_DragEnter);
msg_textBox.DragDrop += new DragEventHandler(msg_DragDrop);
}
void msg_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
void msg_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
StringBuilder buffer = new StringBuilder();
foreach (string filename in files)
{
try
{
string text = File.ReadAllText(filename);
buffer.Append(text);
}
catch (Exception ex)
{
string errMsg = string.Format("cannot read the file\"{0}\" error: {1}", filename, ex.Message);
MessageBox.Show(errMsg, "Reading file error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
msg_textBox.Text = buffer.ToString();
}
Somewhere you have set msg_textBox.EnableAutoDragDrop = true, either in your designer window or your code. You need to set this to false. You do still need to set AllowDrop = true.
When set to true, the winforms RichTextBox provides standard behaviors for drag-and-drop events, to which your custom handlers are added. If you don't want the standard behavior, you have to completely roll your own handlers. (The standard behavior for a dropped text file is OLE embedding. If you double click on the icon, notepad launches.)
I know this is an old post, but after trying to find a solution for the same problem, I ended up with a work around/solution
The DragDrop Method should be:
private void RTB_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
String[] filename = (String[])e.Data.GetData(DataFormats.FileDrop);
RTB.Text = fileName[0]; //this is where the text is
}
Now, if you have a RTB TextChanged method defined (RTB is rich text box), get the RTB.Text, then set the RTB.Text.
This seems to erase the unwated icons in the RTB
Example:
private void RTB_TextChanged(object sender, EventArgs e)
{
string currentText = RTB.Text.ToString();
RTB.Text = currenText;
}
Note: You have to go to the RTB properties and click on the lightning bolt icon (Events) and point the TextChanged Event to the RTB_TextChanged method you defined. Or just double click on the EventName and it'll create a new method for you to complete the method.
Icon placement; Occurs when the Msg_DragDrop method ends, depending on the e.Effect property.
Add this code to the end of the method=>
E.Effect = DragDropEffects.None;
void msg_DragDrop(object sender, DragEventArgs e)
{
//your code here
e.Effect = DragDropEffects.None;
}

C# - Create dynamic buttons and events

I have a form with a tabControl and inside of each tab is a flowLayoutPanel where I can drag and drop files and a button is created for each dropped file. Afterwards when I click on a button, the file that i dropped should open. I have managed to do this for one file only.. My problem is how can I tell which button was clicked and to open the file/app stored in the path for each button.. How can I differentiate in the button_click event the clicked button and the path of the app to open?
Code for this part so far:
Process myProcess = new Process();
string path_app;
public Form1()
{
InitializeComponent();
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.All;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
}
}
private void button_Click(object sender, System.EventArgs e)
{
myProcess.StartInfo.FileName =path_app;
myProcess.Start();
}
Also my tabControl has the possibility to add new tabs but how can I get the selected tab and the inside flowLayoutPanel to know where to create the button?
And by the way, is there a problem of how I open the files? I understood that i have to take into consideration the working directory..
Thank you for your help!
You can utilize Tag property of the Button:
void Form1_DragDrop(object sender, DragEventArgs e)
{
foreach (String s e.Data.GetData(DataFormats.FileDrop))
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
// Add to Tag any data you want to pin to the button
button.Tag = path_app;
}
}
private void button_Click(object sender, System.EventArgs e)
{
// Obtain via Tag
String path_app = ((sender as Button).Tag as String);
myProcess.StartInfo.FileName = path_app;
myProcess.Start();
}
You could use button.Tag = "theFancyPath" and in the EventHandler cast the object sender as Button to access the Tag property.
If you need more then you could inherit from Button:
public class ButtonWithPathProperty : Button
{
public FileInfo PathToOpen { get; private set; }
public ButtonWithPathProperty(FileInfo path)
{
PathToOpen = path;
this.Click += new EventHandler(this.button_Click);
}
private void button_Click(object sender, System.EventArgs e)
{
var yourPath = this.PathToOpen;
}
}
This is not tested btw :)

Get the path of a file dragged into a Windows Forms form

I am developing an application which requires the user to drag a file from Windows Explorer into the application window (Windows Forms form). Is there a way to read the file name, path and other properties of the file in C#?
You can catch the DragDrop event and get the files from there. Something like:
void Form_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
//more processing
}
you Should Use Two Events
1) DragDrop
2) DragEnter
Also enable "AllowDrop" Property of Panel/form to true.
private void form_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void form_DragDrop(object sender, DragEventArgs e)
{
string[] filePaths= (string[])e.Data.GetData(DataFormats.FileDrop, false);
}

Categories

Resources