Screen.PrimaryScreen.DeviceName中的字符无效

时间:2014-11-05 08:49:45

标签: c# .net windows-xp

我在Windows XP上发现了.Net中的一个错误。字符串值Screen.PrimaryScreen.DeviceNameScreen.AllScreens[]在末尾包含额外的字符(空值和来自内存缓冲区的垃圾)。 Windows 7中不存在该问题。问题是 - 是否有Windows Update(KB)修复此问题?

试试这个(在XP上):

Text = string.Format("{0}: {1}",
       Screen.PrimaryScreen.DeviceName.Length, 
       Screen.PrimaryScreen.DeviceName);

通常结果是:31: \\.\DISPLAY1。正确的长度是12而不是31.在\0之后有Display1因此字符串看起来没问题,但是对于字符串比较它是错误的。

1 个答案:

答案 0 :(得分:0)

似乎这是.Net / XP中的错误,Windows更新无法修复。我的解决方案是:

string dev = Screen.PrimaryScreen.DeviceName;
int eos = dev.IndexOf( '\0' );
if ( eos != -1 )
  dev = dev.Substring( 0, eos );