Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use command line arguments in my C# console app?

I am writing a url shortener app and I would like to also create a console app with C# to push the URLs to a WCF service which I have also created.

WCF app will shorten the url on this URI;

http://example.com/shorten/http://exaple.com

so what I want is just that.

My console exe file will be sitting inside c:\dev folder and on Windows command line, I would like to do this;

c:\dev>myapp -throw http://example.com

with this method I would like to talk to that service. there is no problem on talking part. But the problem is how can I supply this -throw thing on the command line and get a response and put that response on the command line and supply a method to copy that to the clipboard. Am I asking too much here? :S I don't know.

Could you direct me somewhere that I can find information on that or could u please give me an example code of this?

Thanks.

EDIT : I have tried the following code;

    class Program {

    static void Main(string[] args) {

        if (args[0] == "-throw") {

            System.Windows.Forms.Clipboard.SetDataObject(args[1]);
            Console.WriteLine(args[1] + " has been added to clipboard !");
            Console.ReadLine();

        }

    }
}

and I received the following error;

C:\Apps\ArgsTry\ArgsTry\bin\Debug>ArgsTry -throw man

Unhandled Exception: System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensur e that your Main function has STAThreadAttribute marked on it. at System.Windows.Forms.Clipboard.SetDataObject(Object data, Boolean copy, In t32 retryTimes, Int32 retryDelay) at System.Windows.Forms.Clipboard.SetDataObject(Object data) at ArgsTry.Program.Main(String[] args) in c:\apps\ArgsTry\ArgsTry\Program.cs: line 14

C:\Apps\ArgsTry\ArgsTry\bin\Debug>

like image 946
tugberk Avatar asked Apr 18 '11 09:04

tugberk


People also ask

How do I run command line arguments?

A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.

How do I add a command line argument to a program?

Right-click on the shortcut and choose Properties. In the target-field add command line parameters after the program file name. For example: %SystemRoot%\system32\notepad.exe C:\test1. txt.


1 Answers

Passing arguments to a console application is easy:

using System;

public class CommandLine
{
   public static void Main(string[] args)
   {
       for(int i = 0; i < args.Length; i++)
       {
           if( args[i] == "-throw" )
           {
               // call http client args[i+1] for URL
           }
       }
   }
}

As for the clipboard, see:

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

like image 149
Blazes Avatar answered Oct 05 '22 12:10

Blazes