The clipboard is automatically cleared when the C # program exits

I want the contents of the clipboard in a C # program to be saved when I exit the program.

For example, in a simple Winform application with the following button click event:

  private void button1_Click( object sender, EventArgs e )
  {
     string preResultText = Clipboard.GetData( DataFormats.StringFormat ) as string;

     DataObject dataObject = new DataObject();
     dataObject.SetData( DataFormats.StringFormat, true, "test" );
     Clipboard.SetDataObject( dataObject );

     string resultText = Clipboard.GetData( DataFormats.StringFormat ) as string;

  }

preResultText should be the same as resultText when the program runs twice and the button runs once during each run.

Instead, I get:

preResultText = null

resultText = "test"

The boolean parameter in SetData seems to be designed to solve this problem, but not on my machine. I read elsewhere that this is not the default behavior, but it still leaves me wondering why this is happening. Suggestions?

+4
source share
1

, copy true SetDataObject:

Clipboard.SetDataObject(dataObject, true);

GetText SetText:

string preResultText = Clipboard.GetText();

Clipboard.SetText("test");

string resultText = Clipboard.GetText();
+2

All Articles