跨 RDC (.NET) 的自定义剪贴板数据格式

发布于 2024-07-15 18:47:03 字数 1289 浏览 5 评论 0原文

我正在尝试将自定义对象从 RDC 窗口复制到主机(我的本地)计算机中。 它失败。 以下是我用于 1) 复制和 2) 粘贴的代码:

1) 远程(在 Windows XP 上运行的客户端,通过 RDC 访问):

            //copy entry
            IDataObject ido = new DataObject();
            XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard));
            StringWriter sw = new StringWriter();
            x.Serialize(sw, new EntryForClipboard(entry));
            ido.SetData(typeof(EntryForClipboard).FullName, sw.ToString());
            Clipboard.SetDataObject(ido, true);

2) 本地(在本地 Windows XP x64 工作站上运行的客户端):

                //paste entry
                IDataObject ido = Clipboard.GetDataObject();
                DataFormats.Format cdf = DataFormats.GetFormat(typeof(EntryForClipboard).FullName);

                if (ido.GetDataPresent(cdf.Name)) //<- this always returns false
                {
                    //can never get here!
                    XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard));
                    string xml = (string)ido.GetData(cdf.Name);
                    StringReader sr = new StringReader(xml);
                    EntryForClipboard data = (EntryForClipboard)x.Deserialize(sr);
                }

它在虽然是同一台机器。

有什么提示吗?

I'm trying to copy a custom object from a RDC window into host (my local) machine. It fails.
Here's the code that i'm using to 1) copy and 2) paste:

1) Remote (client running on Windows XP accessed via RDC):

            //copy entry
            IDataObject ido = new DataObject();
            XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard));
            StringWriter sw = new StringWriter();
            x.Serialize(sw, new EntryForClipboard(entry));
            ido.SetData(typeof(EntryForClipboard).FullName, sw.ToString());
            Clipboard.SetDataObject(ido, true);

2) Local (client running on local Windows XP x64 workstation):

                //paste entry
                IDataObject ido = Clipboard.GetDataObject();
                DataFormats.Format cdf = DataFormats.GetFormat(typeof(EntryForClipboard).FullName);

                if (ido.GetDataPresent(cdf.Name)) //<- this always returns false
                {
                    //can never get here!
                    XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard));
                    string xml = (string)ido.GetData(cdf.Name);
                    StringReader sr = new StringReader(xml);
                    EntryForClipboard data = (EntryForClipboard)x.Deserialize(sr);
                }

It works perfectly on the same machine though.

Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2024-07-22 18:47:03

您可以检查以下几件事:

  1. 您确定对象的序列化确实将其转换为 XML 吗? 也许输出的 XML 引用了您的内存空间? 尝试查看 XML 文本来查看。
  2. 如果您确实有该对象的序列化 XML 版本,为什么不将该值存储为纯文本而不使用 typeof(EntryForClipboard) ? 像这样的东西:

    XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard)); 
      StringWriter sw = new StringWriter(); 
      x.Serialize(sw, new EntryForClipboard(entry)); 
      Clipboard.SetText(sw.ToString(), TextDataFormat.UnicodeText); 
      

    然后,您在客户端程序中所要做的就是检查剪贴板中的文本是否可以反序列化回您的对象。

There are a couple of things you could look into:

  1. Are you sure the serialization of the object truely converts it into XML? Perhaps the outputted XML have references to your memory space? Try looking at the text of the XML to see.
  2. If you really have a serialized XML version of the object, why not store the value as plain-vanilla text and not using typeof(EntryForClipboard) ? Something like:

    XmlSerializer x = new XmlSerializer(typeof(EntryForClipboard));
    StringWriter sw = new StringWriter();
    x.Serialize(sw, new EntryForClipboard(entry));
    Clipboard.SetText(sw.ToString(), TextDataFormat.UnicodeText);
    

    And then, all you'd have to do in the client-program is check if the text in the clipboard can be de-serialized back into your object.

百合的盛世恋 2024-07-22 18:47:03

好的,找到问题所在了。
使用自定义格式通过 RDC 进行复制时,自定义格式名称会被截断为 16 个字符。
该行中的

ido.SetData(typeof(EntryForClipboard).FullName, sw.ToString());

格式名称相当长。

当我在主机上接收复制的数据时,可用的格式具有我的自定义格式,但被截断为 16 个字符。

IDataObject ido = Clipboard.GetDataObject();
ido.GetFormats(); //used to see available formats.

所以我只是使用了更短的格式名称:

//to copy
ido.SetData("MyFormat", sw.ToString());
...
//to paste
DataFormats.Format cdf = DataFormats.GetFormat("MyFormat");
if (ido.GetDataPresent(cdf.Name)) {
  //this not works
  ...

Ok, found what the issue was.
Custom format names get truncated to 16 characters when copying over RDC using custom format.
In the line

ido.SetData(typeof(EntryForClipboard).FullName, sw.ToString());

the format name was quite long.

When i was receiving the copied data on the host machine the formats available had my custom format, but truncated to 16 characters.

IDataObject ido = Clipboard.GetDataObject();
ido.GetFormats(); //used to see available formats.

So i just used a shorter format name:

//to copy
ido.SetData("MyFormat", sw.ToString());
...
//to paste
DataFormats.Format cdf = DataFormats.GetFormat("MyFormat");
if (ido.GetDataPresent(cdf.Name)) {
  //this not works
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文