Accessing Clipboard in console Application

  • Thread starter Thread starter The.Relinator
  • Start date Start date
T

The.Relinator

I need to retrieve a string from the clipboard in a console
application, I have tried many methods but all seem to return a null
value, any help regarding this issue would be greatly appreciated
 
The following works just fine for me; note you need to add a reference
to System.Windows.Forms, but that is not an issue. It is still a
console app:

[STAThread] // for OLE
static void Main(string[] args) {
Console.WriteLine("Was: " + (Clipboard.GetText() ?? ""));
Clipboard.SetText(DateTime.Now.ToString());
Console.WriteLine("Now: " + (Clipboard.GetText() ?? ""));
}

Marc
 
I tried the following, however it does not seem to work, could someone
please shed some light on the situation. This code is found in a dll,
and the application is a console.

StreamWriter sw = new StreamWriter("temp.txt");
sw.Write(Clipboard.GetText() ?? "");
sw.Close();
_Return[0] = "Contents Loaded";
 
The fact that it is a dll shouldn't make any difference (after Fusion
has kicked in). And (to ask the obvious) is there someting in your
clipboard?

**Can you reproduce this in a short, complete program?**

I think something else in your system (that you aren't showing) is
breaking this - for instance are you switching identities (perhaps to
one with an empty clipboard)? Perhaps some other part of the system is
clearing the clipboard for you?

Marc
 
<console app>

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using commandsSSP;

namespace consoleSSP
{
class Program
{
static void Main(string[] args)
{
string myCommand;
do
{
myCommand = Console.ReadLine();

switch (myCommand)
{
case "quit":
File.Delete("temp.txt");
System.Environment.Exit(1);
break;
default:
command Com = new command();
string[] response = Com.execCommands(myCommand,
"console");
int x = 0;
while (response[x] != null)
{
Console.Write("\n " + response[x] +
"\n");
x++;
}
break;
}
} while (myCommand != "CrashAndBurn");
}
}
}

<class library>

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;

namespace commandsSSP
{
public class command
{
string[] _Args = new string[100];
int ArgCount;
string[] _Return = new string[100000];
string _Command;
string[] _ReturnFinal;
public string[] execCommands(string commandString, string
appType)
{
string[] temp;
temp = commandString.Split(' ');
_Command = temp[0];

switch (_Command)
{

case "load":
_Args = temp[1].Split('|');
ArgCount = temp[1].Split('|').Length;
consoleLoad(_Args, true);
break;

default:
_Return[0] = "Invalid Command";
break;
}

return _ReturnFinal = _Return;
}

public void consoleLoad(string[] _Args, bool served)
{
if (_Args[0] == "clipboard")
{
StreamWriter sw = new StreamWriter("temp.txt");
sw.Write(Clipboard.GetText() ?? "");
sw.Close();
_Return[0] = "Contents Loaded";
}
}

}
}


I hope that is enough information
 
Clipboard works through OLE; note in my example:

[STAThread] // for OLE

Add this; all is well. If you really need an MTA or whatever you will
need to manage a second STA thread manually.

Marc
 
Marc, could you please show me exactly where I would put that on the
simple application I posted above Thankyou.
 
Never mind, I was puting it in my class library, thankyou so much for
your help Marc, I really appreciate it
 
On the Main method in the console exe - but you are right in that it is
untidy in that it is hard to keep this Main attribute together with
class library usage. If it needs to be robust (i.e. work from any
process), you would need to check the apartment mode of the current
thread (in the library), and if it isn't STA, invoke an STA thread to
do this bit of work.

Best of luck,

Marc
 
Back
Top