473,802 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21480
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.WriteLi ne("Was: " + (Clipboard.GetT ext() ?? ""));
Clipboard.SetTe xt(DateTime.Now .ToString());
Console.WriteLi ne("Now: " + (Clipboard.GetT ext() ?? ""));
}

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("t emp.txt");
sw.Write(Clipbo ard.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.Collecti ons.Generic;
using System.Text;
using System.IO;
using commandsSSP;

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

switch (myCommand)
{
case "quit":
File.Delete("te mp.txt");
System.Environm ent.Exit(1);
break;
default:
command Com = new command();
string[] response = Com.execCommand s(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.Collecti ons.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(st ring commandString, string
appType)
{
string[] temp;
temp = commandString.S plit(' ');
_Command = temp[0];

switch (_Command)
{

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

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

return _ReturnFinal = _Return;
}

public void consoleLoad(str ing[] _Args, bool served)
{
if (_Args[0] == "clipboard" )
{
StreamWriter sw = new StreamWriter("t emp.txt");
sw.Write(Clipbo ard.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
1472
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 application needs to be web oriented (ASP.NET) I have a console version that work, but I can’t seem to get the ASP side working. I have included the Forms assembly and I'm able to write to the clipboard via the 3rd party, but the clipboard is...
2
5258
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 control over its functionality). That, however, works fine. To make it a little easier for our end users, I wanted to include a feature that places the path and name of the decrypted file on the clipboard. I was going to do this by using...
2
2933
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 the user can edit it (just like you can do in command.com and cmd.exe)
9
5956
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 scans the image. My applications run as compiled assemblies from our Intranet server and I have setup a Main sub routine in the application with the following line: <STAThread()> Public Sub Main() Note: There is no timer in this application.
2
5643
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 clipboard and get the info I need. For instance, run the console app, and then past the info into a notepad after it runs. The only problem is that I can't seem to access the clipboard from a console app. Is this even possible?
20
3525
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 just need to be pointed toward a resource for accessing the DOM this way. I must be searching on the wrong keywords because so far I can't find anything helpful, even on Microsoft's site. My development environment is Visual Web developer...
7
20128
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: System.Runtime.InteropServices.ExternalException: Requested Clipboard operation did not succeed. at System.Windows.Forms.Clipboard.ThorwIfFailed(Int32 hr) at System.Windows.Forms.Clipbard.SetDataObject(Object data, Boolean copy, Int 32 retryTimes, Int32...
15
12305
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 Clipboard class to get an EnhancedMetafile or MetafilePict object from the Clipboard, use that to create a new Metafile object, and then draw that Metafile object using Graphics.DrawImage. It doesn't seem to be that simple.
3
7307
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 the following error below. i just keep executing until it works. i'm getting the following error:
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10305
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.