Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Passing main() input arguments

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
H. Martins - 16 Sep 2007 11:39 GMT
I need to use command line input to a tinny c# application:

It goes like this:

** program.cs **

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ClipboardImageSaveToFile
{

       static class Program
       {

           /// <summary>
           /// The main entry point for the application.
           /// </summary>

               [STAThread]
               public static void Main(string[] args)
<<<<<<<<<<<<<<<< ADED
               {
                   Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);
                   Application.Run(new frmDefForm());
               }
           }
       }

** Form1.cs **

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ClipboardImageSaveToFile
{
   public partial class frmDefForm : Form
   {
       public frmDefForm()
       {
           InitializeComponent();
       }
       private void frmDefForm_Activated(object sender, EventArgs e)
       {
           if (args.Lenght != 2)    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
FIRST ERROR
           {
               MessageBox.Show("Erro: não forem recebidos 2
argumentos (strFullPathName, strFileType");
           }
           if (Clipboard.GetDataObject() != null)
           {
               IDataObject data = Clipboard.GetDataObject();

               if (data.GetDataPresent(DataFormats.Bitmap))
               {
                   Image image =
(Image)data.GetData(DataFormats.Bitmap, true);

               //    image.Save("W:\\users\\hm\\apirac-db\\teste-
b.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
               //    image.Save("W:\\users\\hm\\apirac-db\\teste-
b.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
               //    image.Save("W:\\users\\hm\\apirac-db\\teste-
b.png", System.Drawing.Imaging.ImageFormat.Png);
                   if (args[1] = "jpeg")
                   {
                       image.Save(args[0],
System.Drawing.Imaging.ImageFormat.Jpeg);
                   }
                   else
                   {
                       MessageBox.Show("Formato de ficheiro requerido
não suportado (apenas JPG)");
                   }
               }
               else
               {
                   MessageBox.Show("O 'clipboard' não contém uma
imagem");
               }
           }
           else
           {
               MessageBox.Show("Nada há no 'clipboard'");
           }
       // fechar aplicação
        Close ();
       }
       private void label1_Click(object sender, EventArgs e)
       {
       }
   }
}

This is an adaptation of something found somewhere. The point is that
I can't figure out how to pass main () arguments to the code in the
second file.

At the moment it says:
The name 'args' does not exist in the current context

I understand the array args[] is unknown in the second file, but I
can't find a way to pass array.

Thanks
H. Martins
Family Tree Mike - 16 Sep 2007 17:14 GMT
In your forms code I would use System.Environment.GetCommandLineArgs.

> I need to use command line input to a tinny c# application:
>
[quoted text clipped - 111 lines]
> Thanks
> H. Martins
Peter Duniho - 16 Sep 2007 20:01 GMT
> [...]
> I understand the array args[] is unknown in the second file, but I
> can't find a way to pass array.

Well, it depends on how you want the Form class to treat the arguments.

Personally, I would make the Form class reasonably generic and either
pass the array referenced by the Main() method's "args" paremeter
directly to the Form (either in the constructor or via a property), or I
would preprocess the args in some useful way (for example, converting
the strings to whatever data type they ultimate represent, parsing
flags, etc.) and pass the resulting data to the Form (again, either in
the constructor or via a property).

However, if you want to strongly tie the Form to the
command-line-argument nature of the application, you certainly could use
the approach suggested by Mike and just put all the code in the Form
class to do all the work.

Pete
H. Martins - 16 Sep 2007 23:01 GMT
Indeed, Mike suggestion was perfect.

The problem is already solved.

Thank you both.
H. Martins

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.