Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipboard error: String conversion error: Illegal byte sequence encounted in the input #15511

Open
xthinh opened this issue Jul 2, 2019 · 1 comment

Comments

@xthinh
Copy link

xthinh commented Jul 2, 2019

Dear All
I have try this code to copy some unicode text:

static void Main(string[] args)
{
if (Clipboard.ContainsText())
{
string st = "";
try
{
st = Clipboard.GetText();
Clipboard.SetText("");
Console.WriteLine(st);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}

But when I copy a string like this:
"Little ghé thăm trường Đại học Quân đội Hoàng gia Saint-Jean (Royal Military College Saint-Jean) bên bờ sông Richelieu, nằm cách Montreal bốn mươi km về phía Nam"
to clipboard then when i run the program, the following error has been thrown:
"String conversion error: Illegal byte sequence encounted in the input."

What should I do to resolve this error?

My system is:
OpenSUSE Leap 15.1, KDE
Mono 5.10.1.47
MonoDevelop 7.8.3

@andrzejlisek
Copy link

andrzejlisek commented May 5, 2022

I have Ubuntu 20.04 and the same problem.

using System;
using System.Text;

namespace TestThr2
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Start");
            string SampleText = "Little ghé thăm trường Đại học Quân đội Hoàng gia Saint-Jean (Royal Military College Saint-Jean) bên bờ sông Richelieu, nằm cách Montreal bốn mươi km về phía Nam";
            System.Windows.Forms.Clipboard.SetText(SampleText);
            Console.WriteLine("Text inserted");
            if (System.Windows.Forms.Clipboard.ContainsText())
            {
                Console.WriteLine("Text exists");
                string Txt_ = System.Windows.Forms.Clipboard.GetText();
                Console.WriteLine("Text retrieved");
            }
            else
            {
                Console.WriteLine("Text not exists");
            }
            Console.WriteLine("Stop");
        }
    }
}

Test scenario:

  1. Open Gedit and create new document.
  2. Write some short text to the document using only latin letters.
  3. Copy the text (Ctrl+A and Ctrl+C).
  4. Paste the text (Ctrl+V) to end of the document.
  5. At this step, in the document document the same text will be repeated.
  6. Clear the document using Ctrl+A and Delete.
  7. Run the code. The code runs without exception. On the console there will be Unicode text.
  8. Paste the text using Ctrl+V to Gedit document.
  9. At this step there will be Unicode text in Gedit.
  10. Remove the line System.Windows.Forms.Clipboard.SetText(SampleText); from the code.
  11. Compile and run the code again.
  12. The text will be retrieved and written to the console.
  13. Copy the text manually. In the Gedit press Ctrl+A followed by Ctrl+C.
  14. Run the code one more time without modification and recompilation.
  15. At this point the Exception on System.Windows.Forms.Clipboard.GetText() will occur.

What is the difference between text sent to clipboard using System.Windows.Forms.Clipboard.SetText(string) and copied manually using Ctrl+C, while both textes are exactly the same?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants