Get location of file present in Clipboard

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = Clipboard.GetData.ToString();
}

I pressed Ctrl+C on a file, not a text. I want set TextBox.Text or a string the location of the file. suppose c:\myfile.abc in Clipboard. I want set text equal to the location/path present in the clipboard.

Upvotes: 0

Views: 245

Answers (1)

Ruben2776
Ruben2776

Reputation: 60

if (Clipboard.ContainsFileDropList()) // If Clipboard has one or more files
{
    var files = Clipboard.GetFileDropList().Cast<string>().ToArray(); // Get all files from clipboard

    if (files != null)
    {
        if (files.Length >= 1)
        {
            string filepath = files[0]; // Get first file from clipboard as a file path
            textBox1.Text = filepath;
        }
    }
}

Upvotes: 1

Related Questions