Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:54:06+00:00 2026-06-15T01:54:06+00:00

I have a .NET 4.0 console application that generates SQL and stores it in

  • 0

I have a .NET 4.0 console application that generates SQL and stores it in a string variable. I want this string to be copied directly to the clipboard.

So far, all my research indicates that the ONLY way this can possibly be done is by adding a reference to System.Windows.Forms. I do not want to add a reference to an assembly that is irrelevant to a console application.

Within the universe in which we currently exist, is there a known method of copying a string of text to the clipboard within a console application that does not involve adding a reference to System.Windows.Forms nor any other assembly whose purpose is irrelevant to a bare-bones console application?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:54:08+00:00Added an answer on June 15, 2026 at 1:54 am

    Platform invoking the clipboard APIs is a possible solution. Example:

    using System.Runtime.InteropServices;
    class Program
    {
        [DllImport("user32.dll")]
        internal static extern bool OpenClipboard(IntPtr hWndNewOwner);
    
        [DllImport("user32.dll")]
        internal static extern bool CloseClipboard();
    
        [DllImport("user32.dll")]
        internal static extern bool SetClipboardData(uint uFormat, IntPtr data);
    
        [STAThread]
        static void Main(string[] args)
        {
            OpenClipboard(IntPtr.Zero);
            var yourString = "Hello World!";
            var ptr = Marshal.StringToHGlobalUni(yourString);
            SetClipboardData(13, ptr);
            CloseClipboard();
            Marshal.FreeHGlobal(ptr);
        }
    }
    

    This is just an example. Adding a little error handling around the code, like checking the return values of the P/Invoke functions, would be a good addition.

    SetClipboardData is the interesting bit. You also want to make sure you open and close the clipboard, too.

    The 13 passed in as the first argument is the data format. 13 means a Unicode string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET console application that needs to generate some HTML files. I
In .NET c# 3.5 I have a console application (A) that references several assemblies(X,
I have an (console, .NET 3.5, C#) application that I'm trying to call from
I have a server application (singleton, simple .NET console application) that talks to a
I have a console application that is server based. I only want 1 instance
i have a vb .net 2008 console application that generate a pdf based on
I have a simple piece of code in a .NET console application that tries
Possible Duplicate: .Net Console Application that Doesn’t Bring up a Console I have a
I'm building a very simple application .net Console Application that will execute a SQL
I have a .Net console application that uses a QueryString that connects to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.