How to store data into computer memory in C#?

Posted on 2007-09-30
Last Modified: 2013-11-10

In your browser, when you highlight something and do copy (CTRL-C), that highlighted data is stored into memory. And when you paste it (CTRL-V), that data is pasted onto wherever you want.

I want to do something similar in my C# program.
I want to click a button, and some predetermined data will be saved into the memory.
Then I want to paste it via using CTRL-V, to anywhere I want to.

How can this be done in C#?
Any pointers will be appreciated.
Question by:blast010
    1 Comment
    LVL 22

    Accepted Solution

    You need to use the Clipboard. Here is the example from:

    private void button1_Click(object sender, System.EventArgs e) {
        // Takes the selected text from a text box and puts it on the clipboard.
        if(textBox1.SelectedText != "")
           textBox2.Text = "No text selected in textBox1";
     private void button2_Click(object sender, System.EventArgs e) {
        // Declares an IDataObject to hold the data returned from the clipboard.
        // Retrieves the data from the clipboard.
        IDataObject iData = Clipboard.GetDataObject();
        // Determines whether the data is in a format you can use.
        if(iData.GetDataPresent(DataFormats.Text)) {
           // Yes it is, so display it in a text box.
           textBox2.Text = (String)iData.GetData(DataFormats.Text);
        else {
           // No it is not.
           textBox2.Text = "Could not retrieve data off the clipboard.";


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now