C#文件文件夹拖拽功能的实现

在界面中添加一个TextBox控件,或其他支持拖拽属性的控件。

属性:


事件:


private void Form_DragEnter(object sender, DragEventArgs e)
{
    dragEnter(e);
}

private void Form_DragDrop(object sender, DragEventArgs e)
{
    TextBox textBox = sender as TextBox;
    textBox.Text = dragDrop(e);
}

/// <summary>
/// 文件拖进事件处理:
/// </summary>
public void dragEnter(DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))    //判断拖来的是否是文件
        e.Effect = DragDropEffects.Link;                //是则将拖动源中的数据连接到控件
    else e.Effect = DragDropEffects.None;
}

/// <summary>
/// 文件放下事件处理:
/// 放下, 另外需设置对应控件的 AllowDrop = true; 
/// 获取的文件名形如 "d:\1.txt;d:\2.txt"
/// </summary>
public string dragDrop(DragEventArgs e)
{
    StringBuilder filesName = new StringBuilder("");
    Array file = (System.Array)e.Data.GetData(DataFormats.FileDrop);//将拖来的数据转化为数组存储

    foreach (object I in file)
    {
        string str = I.ToString();

        System.IO.FileInfo info = new System.IO.FileInfo(str);
        //若为目录,则获取目录下所有子文件名
        if ((info.Attributes & System.IO.FileAttributes.Directory) != 0)
        {
            str = getAllFiles(str);
            if (!str.Equals("")) filesName.Append((filesName.Length == 0 ? "" : ";") + str);
        }
        //若为文件,则获取文件名
        else if (System.IO.File.Exists(str))
            filesName.Append((filesName.Length == 0 ? "" : ";") + str);
    }

    return filesName.ToString();
}

/// <summary>
/// 判断path是否为目录
/// </summary>
public bool IsDirectory(String path)
{
    System.IO.FileInfo info = new System.IO.FileInfo(path);
    return (info.Attributes & System.IO.FileAttributes.Directory) != 0;
}

/// <summary>
/// 获取目录path下所有子文件名
/// </summary>
public string getAllFiles(String path)
{
    StringBuilder str = new StringBuilder("");
    if (System.IO.Directory.Exists(path))
    {
        //所有子文件名
        string[] files = System.IO.Directory.GetFiles(path);
        foreach (string file in files)
            str.Append((str.Length == 0 ? "" : ";") + file);

        //所有子目录名
        string[] Dirs = System.IO.Directory.GetDirectories(path);
        foreach (string dir in Dirs)
        {
            string tmp = getAllFiles(dir);  //子目录下所有子文件名
            if (!tmp.Equals("")) str.Append((str.Length == 0 ? "" : ";") + tmp);
        }
    }
    return str.ToString();
}

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现网盘中的拖拽下载功能,可以使用C#中的DragDrop事件来实现。以下是一个简单的实现步骤: 1. 在窗体中添加一个ListBox控件,用于显示文件列表。 2. 设置ListBox的AllowDrop属性为true,允许控件接受拖放事件。 3. 实现ListBox的DragEnter事件,在拖入文件时判断是否合法,如文件类型、大小等。 4. 实现ListBox的DragDrop事件,在拖入文件后获取文件路径,进行下载操作。 下面是一个简单的实现示例: ```csharp private void listBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { // 判断是否是文件 string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string file in files) { FileInfo fileInfo = new FileInfo(file); if (fileInfo.Attributes == FileAttributes.Directory) { // 如果是文件夹,不允许拖放 e.Effect = DragDropEffects.None; return; } } e.Effect = DragDropEffects.Copy; } } private void listBox1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string file in files) { // 下载文件操作 // ... listBox1.Items.Add(file); // 将文件路径添加到列表中显示 } } } ``` 当用户将文件拖动到ListBox控件中时,会触发DragEnter事件,此时判断文件类型、大小等是否符合要求,如果合法则设置拖放效果为Copy。当用户松开鼠标时,会触发DragDrop事件,此时获取文件路径并进行下载操作,最后将文件路径添加到ListBox中显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值