You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过System.Windows.Clipboard获取复制内容的原始数据源?

Can System.Windows.Clipboard retrieve the original source of copied text (file, website, folder)?

Great question! Let’s break down what’s possible here—since the answer depends entirely on the type of content you’re copying and how the source application handles clipboard data.

1. Copied files/folders: Reliable source retrieval

When you copy files or folders from File Explorer (or other file managers), the clipboard actually stores the full file system paths of those items. You can easily retrieve these using Clipboard.GetFileDropList():

using System.Collections.Specialized;
using System.Windows;

// Check if the clipboard contains file drop data
if (Clipboard.ContainsFileDropList())
{
    StringCollection filePaths = Clipboard.GetFileDropList();
    foreach (string path in filePaths)
    {
        // This path is the original source of the copied file/folder
        MessageBox.Show($"Original source: {path}");
    }
}

This works consistently because file managers explicitly add path data to the clipboard in a standard format.

2. Copied text from websites: Unreliable, but sometimes possible

When you copy text from a web browser, some browsers include the HTML version of the copied content in the clipboard (alongside plain text). In rare cases, this HTML data might include metadata about the source URL (like a <base> tag or hidden comments), but this isn’t a standardized behavior.

You can check for HTML data and try parsing it, but results will vary by browser and website:

if (Clipboard.ContainsData(DataFormats.Html))
{
    string htmlClipboardData = Clipboard.GetData(DataFormats.Html) as string;
    // The HTML data usually starts with a header section before the actual copied fragment
    // You can parse this header to look for a "SourceURL" field (some browsers include this)
    int sourceUrlIndex = htmlClipboardData.IndexOf("SourceURL:");
    if (sourceUrlIndex != -1)
    {
        int endIndex = htmlClipboardData.IndexOf('\n', sourceUrlIndex);
        string sourceUrl = htmlClipboardData.Substring(sourceUrlIndex + 10, endIndex - sourceUrlIndex - 10).Trim();
        MessageBox.Show($"Possible source website: {sourceUrl}");
    }
}

Note: This won’t work for all browsers or websites—many don’t include this metadata, so don’t rely on it for critical functionality.

3. Plain text copied from apps (like Notepad, Word, etc.): No built-in source data

When you copy plain text from most desktop apps, the clipboard only stores the text itself—no metadata about where it came from. The source application would have to intentionally add extra data to the clipboard (like a custom data format with the file path) for you to retrieve it, and this isn’t a common practice.

In short:

  • Files/folders: You can reliably get the original source path.
  • Website text: You might be able to parse a source URL, but it’s hit-or-miss.
  • Plain text from apps: No way to get the source unless the app explicitly provides it.

内容的提问来源于stack exchange,提问作者user7425470

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:36:25