473,387 Members | 1,650 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,387 software developers and data experts.

building a program that accepts commandline parameters

HaLo2FrEeEk
404 256MB
I'd like to write a program to assist me in upoloading files to my site. I know how to upload the files using C#, I just don't know how to have an interface and still accept commandline parameters. I'd like to be able to select multiple files in windows explorer and right click them and have my program be listed in the context menu, so I select that option and it opens the program and gives me options for where each file will go. How can I accomplish something like this? I know it's possible, windows media player accepts commandline parameters and also has a gui, so I know it can be done.
Dec 27 '09 #1
8 4299
GaryTexmo
1,501 Expert 1GB
Windows programs still have a main thread. Your average C# project puts this in Program.cs these days, this is what sets up and runs your application. If you add a string array argument to this main thread, you can then access them as the command line arguments, similar to a console program.

(this is what I got when creating a new Windows Form Application in C# 2008 Express Edition. I added string [] args and the foreach myself for demonstration purposes)
Expand|Select|Wrap|Line Numbers
  1.     static class Program
  2.     {
  3.         /// <summary>
  4.         /// The main entry point for the application.
  5.         /// </summary>
  6.         [STAThread]
  7.         static void Main(string [] args)
  8.         {
  9.             foreach (string str in args)
  10.                 Console.WriteLine(str);
  11.  
  12.             Application.EnableVisualStyles();
  13.             Application.SetCompatibleTextRenderingDefault(false);
  14.             Application.Run(new Form1());
  15.         }
  16.     }
As for using a context menu, it looks like it's a registry entry. I did some googling and found a program that will let you set them up from codeproject, but you can probably just as easily have your program set a registry key when it starts up, or as a part of your installation procedure.

http://www.codeproject.com/KB/cs/appendmenu.aspx
Dec 28 '09 #2
HaLo2FrEeEk
404 256MB
Thank you for the reply, I'll definitely take a look at this when I get home. I hope this will be one of the projects that I actually finish, it'll be so useful. Right now I'm using the pscp.exe commandline utility from the putty shell package along with a batch file to upload files. I just put a shortcut to the batch file in the sendto folder. Right click a file > send to > ftp, it pops up asking where I want to put the file and the uploads it there. Only probloem with this is I can only do files and only one at a time, so this little app will help a lot.
Dec 28 '09 #3
HaLo2FrEeEk
404 256MB
Ok, so I've gotten this to work. I modified the code you posted a bit:

in Program.cs:
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.         {
  3.             Application.EnableVisualStyles();
  4.             Application.SetCompatibleTextRenderingDefault(false);
  5.             Application.Run(new Form1(args));
  6.         }
And in Form1.cs:
Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2.     {
  3.         string[] commands;
  4.  
  5.         public Form1(string[] args)
  6.         {
  7.             commands = args;
  8.             InitializeComponent();
  9.             parseCommands(args);
  10.         }
  11.  
  12.         public void parseCommands(string[] args)
  13.         {
  14.             foreach (string str in args)
  15.             {
  16.  
  17.             }
  18.         }
  19.     }
That just pops up a messagebox for each item passed as a commandline argument. I tested it and it works with dragging files onto the program executable as well as directly from the commandline. Now I just need to figure out how to have commandline variables, for example:

program.exe /user username /pass password [file array]

Any ideas?
Dec 29 '09 #4
GaryTexmo
1,501 Expert 1GB
Ahhhh, parsing strings, always fun :) I could tell you how to do it, but this is something that you'll probably best want to figure out yourself, so maybe I'll give you some rough guidelines by examining your expected command line.

* The parameter args already comes in as the command line, separated into spaces (I even believe they are trimmed), but the string class is quite versatile so if you look at the methods it has, I bet you can figure out how to separate a string into tokens, via a tokenizer (ie, turn "this is a test" into {"this", "is", "a", "test"}).

* You are looking for specific commands in your command line, so there's no need to get tricky with how you match the tokens.

* If a token is a command, the very next token is the parameter for that command. If the token is not a command (per your description), the token is a file in the file array.

* For this you need to decide if you want to support something like "program.exe file1 /user username file2 /pass password file3 ... fileN" or "program.exe /pass password file1 file2 ... fileN /user username" vs only allowing the explicit order of "program.exe /user username /pass password file1 file2 ... fileN". It's easy to set up to allow any order, but you might want to force it to be one order, at least for where the file array sits, so it's not confusing to the user. This is something that's really your choice.

Anyway, I hope that gets you started. Feel free to ask me if you have any more questions.
Dec 29 '09 #5
HaLo2FrEeEk
404 256MB
Well the "user" is most likely only going to be me, I just like to make things versatile just in case. I figured out that if I put each argument in quotes then it shows up as a single parameter. So program.exe "/user username" "/pass password", would pass the arguments /user username and /pass password, I could set an explicit order and then just look for those commands. I do have pretty extensive parsing strings, I made a class in php to pull some information from a remote webpage and parse it into a very versatile array for each different piece of information. When I get home I'll have an other play with this and see what I can get. I'll start by just passing the user and pass args and seeing if I can get their values and show them in a messagebox.
Dec 29 '09 #6
HaLo2FrEeEk
404 256MB
So I managed this:

http://infectionist.com/misc/Commandline_Test.zip

If you run the program from the commandline with the options

/user-[username] and
/pass-[password]

the program will save those to the "string username" and "string passwor"d variables, If only one or neither are set then when you press the "Values" button (which only activates when files are passed to the program) the program prompts you for username and password. The program won't print the values until the proper username and password is entered. The username and password required is "username" and "password" (original, I know, but it's simple and it works.)

So try running the program from Command Prompt like this:

"Commandline Test" /user-username /-pass-password

Without any files the program will simply open up with a disabled button that says "Values". If you drag some files onto the executable it'll run with those files as commandline argments. Up to 7 files will be displayed in the window at one time, any after that will cause scrollbars to appear. When files are passed to it without the username and password, you'll have to enter the username and password before being able to use the "Values" button.

I'm sure it's really hacked up and there's a lot better way to do all this, but it works for now. It's basically a proof of concept, incorporating everything I want my full program to use. Username and password entry to log into the FTP server, passing files to be uploaded, and iterating through the array of textboxes. I should definitely be able to get this working how I want it to, I just have to incorporate the actual FTP uploading and being able to dynamically add or remove files from the list. I'm thinking I should try and make it drag-n-drop compatible so all I have to do is drag a file into the window for it to add it to the list.

So, any comments on the program? Anything you notice that I should change?
Dec 31 '09 #7
GaryTexmo
1,501 Expert 1GB
I had to do some mucking about to get it to work for me (as I didn't give it files, just played with it through the debugger), but it seems to work fine. I'm not sure what you're looking for by way of comments though.

There are things I'd definitely do differently... definitely to make the program safer and possibly more efficient, but to be honest that's a personal choice and would be nitpicky :) If you're interested, contact me via PM and we can chat about it, but as far as this thread goes you look like you've got something workable so that's that.

Nice work :)
Jan 1 '10 #8
HaLo2FrEeEk
404 256MB
Well like I said this is simply the testing ground, where I can put all the ideas into play and see if/how they work. The final program will be safer, more secure, faster, etc. I just wanted to see how everything worked and worked together.

Try running the program from /bin/release, you can drag the "Commandline Test.pdb" file onto the executable and see what it does, the debugger won't let you do that. I also made a bunch of copies of the pdb file and tried it with like, 10 files at a time, handles quite well.
Jan 1 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Slavik | last post by:
All libraries were installed (precompiled) This is FreeBSD 5.1 installed zlib, installed jpeg and png libraries (in default directories) GD 2.0.11 source is in /usr/gd-2.0.11 (compiled and...
2
by: Alexander Gausa | last post by:
hi i want to start a php programm from commandline and pass some parameters to the programm. but the programm did not recognize the parameters. the commandline ist: php liste.php para1=one...
8
by: Peter Nolan | last post by:
Hi All, I have written some software that performs ETL processing to load data warehouses. Each program accepts a set of parameters and returns 0 or 1 to the win/unix shell to indicate success or...
4
by: esmith2112 | last post by:
Having a bear of a time trying to implement replication under 8.2 environment. I've created all of the control structures on both source and target database and can actually see data being staged...
0
by: michaeldavidcox | last post by:
I have one project that is giving me an error but only when I build it outside of VS 2005 IDE: C:\Code\GUI\Form1.vb(731) : error BC31094: Implementing class 'DSO.ServerClass' for interface...
3
by: balakrishnan.dinesh | last post by:
hi frndz, As we know that, we can pass command line agrument for C using "scanf" commands, So as same as that, Is there any way to pass those commandline arguments through php code to C and...
8
by: barcaroller | last post by:
Is there a recommended method of parsing program input parameters/options in C++ programs? The three methods that I know of are: - C's getopt() and getopt_long() - GNU C++ GetOpt class -...
0
by: mabra | last post by:
Hi All! After hours of searching the net, I found nothing really substantial about this. For me, passing username and password from the commandline, is not very unsecure, but there is no way...
4
by: ramshankaryadav | last post by:
Hi, I'm facing a problem while building a project through MSBuild, this project is a part of a solution which has several other projects on which it depends, but I want to build this project...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.