473,326 Members | 2,102 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,326 software developers and data experts.

Passing main() input arguments

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(fals e);
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

Sep 16 '07 #1
3 7902
In your forms code I would use System.Environment.GetCommandLineArgs.

"H. Martins" wrote:
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(fals e);
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

Sep 16 '07 #2
H. Martins wrote:
[...]
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
Sep 16 '07 #3
Indeed, Mike suggestion was perfect.

The problem is already solved.

Thank you both.
H. Martins
Sep 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
3
by: Anon Email | last post by:
Hi people, In the following code, in the main method, why is it possible to pass "input" as an argument to the method TheCalculator.Execute()? This method is supposed to accept references to...
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
5
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
4
by: Justine | last post by:
Can anyone help? e.g. when you open a vb project property page, in "Configuration Properties" You will see "Command line arguments" in "Start Options". Does anyone knows how to find a way to do...
17
by: ern | last post by:
I want to pass arguments to the main( ) function of my C program. Currently, I'm using main(char * args) But when I try to print args, I get an unhandled exception error. Is there a better,...
4
by: phoolpreet | last post by:
hi guys i m stuck with the problem of passing arguments to main. we can define argc and argv and put statements in main that use these arguments. but how to set these value while running a...
2
by: william.w.oneill | last post by:
I have an application that takes a few command line parameters. As recommended by others in this group, I'm using a named mutex to ensure that only one instance of the application is running. My...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.