473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Accessing Clipboard in console Application

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

Aug 18 '06 #1
9 21280
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

Aug 18 '06 #2
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";

Aug 18 '06 #3
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

Aug 19 '06 #4
<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

Aug 19 '06 #5
^^^ Thats it

Aug 19 '06 #6
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

Aug 19 '06 #7
Marc, could you please show me exactly where I would put that on the
simple application I posted above Thankyou.

Aug 19 '06 #8
Never mind, I was puting it in my class library, thankyou so much for
your help Marc, I really appreciate it

Aug 20 '06 #9
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

Aug 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill Belliveau | last post by:
Hello all I have the need to copy something to the clipboard via 3rd party type library and then pick it up on the .NET side via the clipboard (all server-side). To complicate the issue, the...
2
by: Roy Gunnarsson | last post by:
Hello all, I have a VERY simple windows application that decrypts data files we receive from a partner. The decryption is handled by a COM component our partner provided us with (thus we have no...
2
by: Jarod_24 | last post by:
I'm working on a console-view for my application wich is kinda a replacement for the orignal cmd.exe in windows xp. I could simply use .Write() to do that, but how do i paste the text so that...
9
by: Tim Frawley | last post by:
I have converted a VB6 application to VB.NET. The old application made extensive use of the Clipboard for copying an Image Name so that it could be pasted into the image capture app when the user...
2
by: Scott Kilbourn | last post by:
Hi, I have a little console app that just does a little bit of processing. The last thing I want it to do before it exists is stick a string onto the clipboard. I will then access the...
20
by: Randall Arnold | last post by:
I've got an asp.net page that dynamically generates an html table on the server side. On the client side, I need to copy the rendered table to the clipboard programmatically (using vbscript). I...
7
by: Dave Booker | last post by:
My Clipboard is getting into some very confused state where I can't even clear it. No applications can access the clipboard in this state, and if I run Clipboard.Clear() I get: ...
15
by: Peter Duniho | last post by:
I'm trying to use .NET and C# to draw a metafile copied to the clipboard by another application (Word 2003 in this case, but it shouldn't matter). I naively thought that I'd be able to use the...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i'm using the following in my console app: My.Computer.Clipboard.SetText(sb.ToString()) i'm getting inconsistent results with the copy. sometimes it works and most of the time i get...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.