When i drag and drop a file into my form it should give:
textBox1.text = the file name (i dont have it, i need it can you help me get it)
textBox2.text = file location (already know how to acquire it from a drag/drop)

I want the actual file name, that is for e.g.
file location- c:/user/folder/subfolder/application.exe
file name- application.exe

private void Form1_DragEnter(object sender, DragEventArgs e)
        {
           if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;

        }

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

so what code do i have to insert for it to give:
textBox1.text = the file name

thanks in advance,

p.s.

private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Browse";
            fdlg.Filter = "All Files.* (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;

            string fullpath = "";
            string filename = "";
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                fullpath = System.IO.Path.GetFullPath(fdlg.FileName);
                filename = System.IO.Path.GetFileName(fdlg.FileName);

                textBox1.Text = filename;
                textBox2.Text = fullpath;

            }
        }

see the string filename ? thats the info i need but instead of a openfiledialog i need it from the drag/drop

Recommended Answers

All 4 Replies

Try

string fileName = "";
    if(e.Data.GetDataPresent(DataFormats.Text)) 
    {
        fileName = (string)e.Data.GetData(DataFormats.Text);
    }

ill check if it works when i get back home

Try

string fileName = "";
    if(e.Data.GetDataPresent(DataFormats.Text)) 
    {
        fileName = (string)e.Data.GetData(DataFormats.Text);
    }

doesnt seem to work =[

fixed:

foreach (string filelocation in (string[])e.Data.GetData(DataFormats.FileDrop))
            {
                textBox2.Text = filelocation;
                textBox1.Text = System.IO.Path.GetFileName(filelocation);
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.