获取剪贴板中的文件位置

问题描述 投票:0回答:1
private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = Clipboard.GetData.ToString();
}

我在文件上按了Ctrl+C,而不是文本。我想设置 TextBox.Text 或一个字符串来表示文件的位置,假设 c:\myfile.abc 中的剪贴板。我想将文本设置为剪贴板中的位置路径。

c# clipboard
1个回答
1
投票
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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.