iversonlee

导航

 

在界面中添加一个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();

 

  1. if (e.Data.GetDataPresent(DataFormats.FileDrop)) //判断拖来的是否是文件
  2.  
    e.Effect = DragDropEffects.Link; //是则将拖动源中的数据连接到控件
  3.  
    else e.Effect = DragDropEffects.None;
  4.  
    }
  5.  
     
  6.  
    /// <summary>
  7.  
    /// 文件放下事件处理:
  8.  
    /// 放下, 另外需设置对应控件的 AllowDrop = true;
  9.  
    /// 获取的文件名形如 "d:\1.txt;d:\2.txt"
  10.  
    /// </summary>
  11.  
    public string dragDrop(DragEventArgs e)
  12.  
    {
  13.  
    StringBuilder filesName = new StringBuilder("");
  14.  
    Array file = (System.Array)e.Data.GetData(DataFormats.FileDrop);//将拖来的数据转化为数组存储
  15.  
     
  16.  
    foreach (object I in file)
  17.  
    {
  18.  
    string str = I.ToString();
  19.  
     
  20.  
    System.IO.FileInfo info = new System.IO.FileInfo(str);
  21.  
    //若为目录,则获取目录下所有子文件名
  22.  
    if ((info.Attributes & System.IO.FileAttributes.Directory) != 0)
  23.  
    {
  24.  
    str = getAllFiles(str);
  25.  
    if (!str.Equals("")) filesName.Append((filesName.Length == 0 ? "" : ";") + str);
  26.  
    }
  27.  
    //若为文件,则获取文件名
  28.  
    else if (System.IO.File.Exists(str))
  29.  
    filesName.Append((filesName.Length == 0 ? "" : ";") + str);
  30.  
    }
  31.  
     
  32.  
    return filesName.ToString();
  33.  
    }
  34.  
     
  35.  
    /// <summary>
  36.  
    /// 判断path是否为目录
  37.  
    /// </summary>
  38.  
    public bool IsDirectory(String path)
  39.  
    {
  40.  
    System.IO.FileInfo info = new System.IO.FileInfo(path);
  41.  
    return (info.Attributes & System.IO.FileAttributes.Directory) != 0;
  42.  
    }
  43.  
     
  44.  
    /// <summary>
  45.  
    /// 获取目录path下所有子文件名
  46.  
    /// </summary>
  47.  
    public string getAllFiles(String path)
  48.  
    {
  49.  
    StringBuilder str = new StringBuilder("");
  50.  
    if (System.IO.Directory.Exists(path))
  51.  
    {
  52.  
    //所有子文件名
  53.  
    string[] files = System.IO.Directory.GetFiles(path);
  54.  
    foreach (string file in files)
  55.  
    str.Append((str.Length == 0 ? "" : ";") + file);
  56.  
     
  57.  
    //所有子目录名
  58.  
    string[] Dirs = System.IO.Directory.GetDirectories(path);
  59.  
    foreach (string dir in Dirs)
  60.  
    {
  61.  
    string tmp = getAllFiles(dir); //子目录下所有子文件名
  62.  
    if (!tmp.Equals("")) str.Append((str.Length == 0 ? "" : ";") + tmp);
  63.  
    }
  64.  
    }
  65.  
    return str.ToString();
  66.  
posted on 2021-03-02 18:10  iversonlee  阅读(541)  评论(0编辑  收藏  举报