Passing main() input arguments

H

H. Martins

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
 
P

Peter Duniho

H. Martins said:
[...]
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

H. Martins

Indeed, Mike suggestion was perfect.

The problem is already solved.

Thank you both.
H. Martins
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top