0

I use VS 2017 and win 10, I am making a simple console application, I want to use the clipboard, for this I include using System.Windows.Forms;

But I get the error: The type or name of the Forms namespace does not exist in the System.Windows space, is there any assembly reference missing?

If I put a point and wait for the intellisense after Windows I do not get as a Forms option.

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

[STAThreadAttribute]
        static void Main(string[] args)
        {
            String comando = "Inicio";
            String respuesta = "";
            //int[] vector;

            Console.WriteLine("CLI asp.net mvc");
            Console.WriteLine("Introduzca su opción");

            do
            {
                comando = Console.ReadLine();

                switch (comando)
                {
                    case "hp":
                        respuesta = "[HttpPost]";
                        break;
                    case "hv":
                        respuesta = "[HttpPost]" + Environment.NewLine + "[ValidateAntiForgeryToken]";
                        break;
                    case "vaft":
                        respuesta = "[ValidateAntiForgeryToken]";
                        break;

                }
                Clipboard.SetText(respuesta);

                Console.WriteLine(respuesta);
            } while (comando != "salir");

            Environment.Exit(1);
        }

Is there another class on github that I can use? or a change on this one ?

In clipboard I get: Clipboard does not exist in the current context

Jhon Hernández
  • 293
  • 5
  • 20
  • @KenWhite OP claims no - https://stackoverflow.com/questions/59146637/i-cant-send-text-to-the-clipboard-on-a-console-applicacion … It's hard to see how OP could not get extremely detailed steps in [console specific answer](https://stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp/34077334#34077334) to produce desired result but clearly OP has some problem... I personally don't think interop is easier than the steps in the linked question... but that's just me. – Alexei Levenkov Dec 03 '19 at 02:17
  • @AlexeiLevenkov: Yeah, I see no explanation of how it *didn't work* or even that the poster tried it. Just a comment that says *didn't work*. I don't see how it could possibly fail, since the answer has 764 upvotes, which is usually a clear indication that it's been used by many people successfully. – Ken White Dec 03 '19 at 02:22
  • @KenWhite: "I´ve tried the usual way but fail, the error says that clipboard is not recognized" A quick check and I wrote my answer - he lacks that namespace reference. – Christopher Dec 03 '19 at 02:25
  • @Christopher: Yes, that namespace is required, which is clearly indicated in the answer to the post I linked (for both WinForms and WPF). The poster needs to read that answer more carefully - posting a new duplicate question because they didn't bother do do so doesn't benefit this site; neither does posting an answer to that duplicate that points out what they missed. You can do that in a comment while voting to close the question as a duplicate. – Ken White Dec 03 '19 at 02:31
  • At the poster: Exception handling is a pet peeve of mine and this one is terrible. It is no wonder you got no error messages to debug this - you blocked and swallowed all of them. You do not only have the usual mistake of catching exception, you actually go out of your way to catch the OOM Exception. There are two articles on Exception handling I link often. Especially the 1st one on what not to catch/handle is important: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ | https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET – Christopher Dec 03 '19 at 02:34
  • 1
    Hernandez400 please make sure to follow every step of linked duplicate. There is very good chance that you either using .NET Core or skipping "add reference" step. Based on information you've provided about first approach it is unlikely SO can help with interop solution in a way that fits SO Q&A format (and so far there is no good indication that standard approach does not work in your case). If you still blocked I'd recommend to re-read [MCVE] guide and start asking about standard solution first as its way more likely you'll get it working. – Alexei Levenkov Dec 03 '19 at 03:41
  • The link above is dead, unfortunately. In a nutshell, the answer is: 1: Create a new project of type "ConsoleApp (.NET Framework). DON'T use Console application for .NET Core- it doesn't support the use of assemblies 2: Right click the project in Solution explorer 3: Click "Add" then "Reference" 4: In the "Reference Managers" page, click on "Assemblies." 5: Locate System.Windows.Forms and check it, then click on "OK." 6: Add the "using System.Windows.Forms" line to your class, and it should work fine. Good Luck – Christian Gibbs Aug 04 '21 at 21:41

1 Answers1

-2

There is a Clipboard Class is in the System.Windows Namespace. Wich I have no doubt will be better then writing your own custom code, that uses Interop. There are some limitationes listed in Introductions however.

It is also listed as a option for IPC. But for that it is not a very usefull or reliable one.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • 1
    You are talking about different one from another namespace... OP is following https://stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp/34077334#34077334... If that answer could not help I doubt the style you've used to answer would be any more helpful... (Also this answer repeats info in the linked post - consider to figure out what OP does not get and answer only that part rather than duplicating info from standard one) – Alexei Levenkov Dec 03 '19 at 02:28
  • @AlexeiLevenkov I updated the question only minutes after your comment. – Christopher Dec 03 '19 at 02:34
  • Christopher - I'm not sure what you expect me to do here. While I don't think your answer is useful to OP or for the site in current form I have not voted on it yet. Are you sure voting is what you want me to do? – Alexei Levenkov Dec 03 '19 at 06:16
  • @AlexeiLevenkov How about "stop being inconsistent"? The accepted answer for what this is a Duplicate for, only Mentions the managed classes: https://stackoverflow.com/a/3546026/3346583 | So it is wrong if I write it, but was always right when somebody else wrote it? – Christopher Dec 03 '19 at 11:52