Copy or Move multiple files C# windows Forms- drag and drop

1
25



Copy files c# windows Forms

CODE:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

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

private void textBox1_DragDrop(object sender, DragEventArgs e)
{
// This just allows you to drop a file into the textbox and display it’s path.
string[] files1 = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string file1 in files1)
textBox1.Text = file1;
}

private void textBox1_DragEnter(object sender, DragEventArgs e)
{
//Drag and drop effect in windows
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
e.Effect = DragDropEffects.All;
}

}

private void textBox2_DragDrop(object sender, DragEventArgs e)
{
string[] files2 = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string file2 in files2)
textBox2.Text = file2;
}

private void textBox2_DragEnter(object sender, DragEventArgs e)
{
//Drag and drop effect in windows
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
e.Effect = DragDropEffects.All;
}

}

private void button1_Click(object sender, EventArgs e)
{

try
{
string dircopyFrom = textBox1.Text;

string[] fileEntries1 = Directory.GetFiles(dircopyFrom);
string dircopyTo = textBox2.Text;

foreach (string file1 in fileEntries1)
{
string filename1 = Path.GetFileName(file1);

File.Copy(file1, dircopyTo + “\” + filename1, true);
}
}
//catches any errors, stops crashes.
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}

}

private void button2_Click(object sender, EventArgs e)
{
try
{
string dircopyFrom = textBox1.Text;

string[] fileEntries1 = Directory.GetFiles(dircopyFrom);
string dircopyTo = textBox2.Text;

foreach (string file1 in fileEntries1)
{
string filename1 = Path.GetFileName(file1);

File.Copy(file1, dircopyTo + “\” + filename1, true);
File.Delete(file1);
}
}
//catches any errors, stops crashes.
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
//All code going in description….

//Thanks

}
}
}

Nguồn:https://wijstaanvooronzegrondrechten.org/

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here