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.
building a program that accepts commandline parameters
Collapse
X
-
Tags: None
-
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)
Code:static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string [] args) { foreach (string str in args) Console.WriteLine(str); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
-
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.Comment
-
Ok, so I've gotten this to work. I modified the code you posted a bit:
in Program.cs:
Code:static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(args)); }
Code:public partial class Form1 : Form { string[] commands; public Form1(string[] args) { commands = args; InitializeComponent(); parseCommands(args); } public void parseCommands(string[] args) { foreach (string str in args) { } } }
program.exe /user username /pass password [file array]
Any ideas?Comment
-
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.ex e file1 /user username file2 /pass password file3 ... fileN" or "program.ex e /pass password file1 file2 ... fileN /user username" vs only allowing the explicit order of "program.ex e /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.Comment
-
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.Comment
-
So I managed this:
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:
"Commandlin e 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?Comment
-
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 :)Comment
-
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 "Commandlin e 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.Comment
Comment