C# How to securely store data in clipboard

T.Zacks 3,996 Reputation points
2022-07-19T07:40:18.537+00:00

if we store anything in clipboard then i want no other application will be able to read my data from clipboard. is it possible?

why people use STA threading model when store data into clipboard ? see the below code
Thread thread = new Thread(() => Clipboard.SetText("String to be copied to clipboard"));
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
thread.Join();

code taken from https://stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp?noredirect=1&lq=1

why we need to call Join() function thread.Join(); after starting thread thread.Start(); ?

Developer technologies | C#
0 comments No comments
{count} vote

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-07-19T08:29:41.233+00:00

    Here are the answers to your question:

    1. If we store anything in clipboard then I want no other application will be able to read my data from clipboard. is it possible? The clipboard, by design is a mechanism for transferring data based on user's command, so the user can access the same data in another application. Considering the following facts as per documentations:
      • The clipboard is a set of functions and messages that enable applications to transfer data. Because all applications have access to the clipboard, data can be easily transferred between applications or within an application.
      • The clipboard is user-driven. A window should transfer data to or from the clipboard only in response to a command from the user. A window must not use the clipboard to transfer data without the user's knowledge.
      While you can encrypt the message with an encryption key which your application knows, but you are basically misusing clipboard. If you are setting something in clipboard, it basically means you want to share it with other applications based on user command.
      If it's something just for your application, you can just keep it your application memory, or in a storage which belong to your application.
    2. Why people use STA threading model when store data into clipboard? As per documentations: The Clipboard class can only be used in threads set to single thread apartment (STA) mode.
    3. Why we need to call Thread.Join() after Thread.Start() The purpose of Join as per documentation: Blocks the calling thread until the thread represented by this instance terminates, while continuing to perform standard COM and SendMessage pumping.
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,441 Reputation points
    2022-07-19T07:54:02.047+00:00

    if we store anything in clipboard then i want no other application will be able to read my data from clipboard. is it possible?

    No, it's not possible. The clipboard is user related and the user can copy the content of clipboard to anywhere.

    1 person found this answer helpful.
    0 comments No comments

  2. RLWA32 49,636 Reputation points
    2022-07-19T08:04:21.867+00:00

    By design the windows clipboard is a resource that is shared by all processes that are running on the same desktop. Consequently each process has access. So not only can other processes access clipboard contents but then can also empty the clipboard. Boom, your stuff is now gone.

    The reason behind using an STA thread is because the underlying code uses the OLE clipboard functions.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.