跨 RDC (.NET) 的自定义剪贴板数据格式
我正在尝试将自定义对象从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查以下几件事:
如果您确实有该对象的序列化 XML 版本,为什么不将该值存储为纯文本而不使用
typeof(EntryForClipboard)
? 像这样的东西:然后,您在客户端程序中所要做的就是检查剪贴板中的文本是否可以反序列化回您的对象。
There are a couple of things you could look into:
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: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.
好的,找到问题所在了。
使用自定义格式通过 RDC 进行复制时,自定义格式名称会被截断为 16 个字符。
该行中的
格式名称相当长。
当我在主机上接收复制的数据时,可用的格式具有我的自定义格式,但被截断为 16 个字符。
所以我只是使用了更短的格式名称:
Ok, found what the issue was.
Custom format names get truncated to 16 characters when copying over RDC using custom format.
In the line
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.
So i just used a shorter format name: