Click here to Skip to main content
15,903,856 members
Home / Discussions / C#
   

C#

 
Questionrandom sql Pin
ujupanmester18-Mar-07 6:10
ujupanmester18-Mar-07 6:10 
AnswerRe: random sql Pin
Mudsoad18-Mar-07 7:07
Mudsoad18-Mar-07 7:07 
GeneralRe: random sql Pin
ujupanmester18-Mar-07 15:02
ujupanmester18-Mar-07 15:02 
Questionopening file with my program Pin
dsl/fahk18-Mar-07 3:59
dsl/fahk18-Mar-07 3:59 
AnswerRe: opening file with my program Pin
Stefan Troschuetz18-Mar-07 4:36
Stefan Troschuetz18-Mar-07 4:36 
AnswerRe: opening file with my program Pin
Wayne Phipps18-Mar-07 5:01
Wayne Phipps18-Mar-07 5:01 
QuestionRe: opening file with my program Pin
dsl/fahk19-Mar-07 15:04
dsl/fahk19-Mar-07 15:04 
AnswerRe: opening file with my program Pin
Wayne Phipps20-Mar-07 10:27
Wayne Phipps20-Mar-07 10:27 
Ok, the pointer should have been enough to get the ball rolling. It showed one way to obtain the file name passed as a command line argument.

What you do from here on is up to you.

Windows Forms applications pass command line arguments in the same way, the only difference is how you reference the arguments.

One way would be to use a member variable to hold the filename. If you make this a public static you can then access if from anywhere within your application. You could do something as follows:
public static string FooFileName = null;

[STAThread]
public static void Main(string[] args)
{
    //check for the required file extension
    if ( args.Length > 0 && args[0].ToLower().EndsWith(".foo") )
    {
        //update the static member withe the file name
        FooFileName = args[0];
    }

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
}

public MainForm()
{
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //check that the filename is not null
    if ( FooFileName != null )
    {
        //do something with the file
        MessageBox.Show( String.Format("FooFile Name: {0}", FooFileName) );
    }
}


Another way would be to create a constructor which allows the filename to be passed as follows:
[STAThread]
public static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    if ( args.Length > 0 && args[0].ToLower().EndsWith(".foo") )
    {
        //pass the the FooFile name to the MainForm constructor
        Application.Run(new MainForm( args[0] ));
    } else {
        //pass nothing to the MainForm constructor
        Application.Run(new MainForm( null ));
    }
}

public MainForm( string FooFileName )
{
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //

    //do something if a FooFileName was passed
    if ( FooFileName != null ) MessageBox.Show( String.Format("FooFile name: {0}", FooFileName) );
}


Does that help??

If so maybe you would consider rating my answer? If not maybe we should consider further discussions via email.


Regards

Wayne Phipps
____________

Time is the greatest teacher... unfortunately, it kills all of its students
View my Blog

QuestionMicrosoft Office Translations Pin
mm31018-Mar-07 3:55
mm31018-Mar-07 3:55 
AnswerRe: Microsoft Office Translations Pin
Ed.Poore19-Mar-07 13:03
Ed.Poore19-Mar-07 13:03 
QuestionProblem receiving data through serial port Pin
naffilk18-Mar-07 2:46
naffilk18-Mar-07 2:46 
AnswerRe: Problem receiving data through serial port Pin
rbirkelbach19-Mar-07 5:03
rbirkelbach19-Mar-07 5:03 
Questionc# word xp textbox Pin
spinis18-Mar-07 0:00
spinis18-Mar-07 0:00 
AnswerRe: c# word xp textbox Pin
Thomas Stockwell19-Mar-07 8:26
professionalThomas Stockwell19-Mar-07 8:26 
QuestionBioinformatic algorithms in C# Pin
bufugus17-Mar-07 23:50
bufugus17-Mar-07 23:50 
AnswerRe: Bioinformatic algorithms in C# Pin
b0ksah19-Mar-07 0:28
b0ksah19-Mar-07 0:28 
QuestionGeneral info passer? Pin
gvanto17-Mar-07 22:39
gvanto17-Mar-07 22:39 
AnswerRe: General info passer? Pin
Stefan Troschuetz17-Mar-07 22:58
Stefan Troschuetz17-Mar-07 22:58 
GeneralRe: General info passer? Pin
gvanto17-Mar-07 23:15
gvanto17-Mar-07 23:15 
GeneralRe: General info passer? Pin
Stefan Troschuetz17-Mar-07 23:39
Stefan Troschuetz17-Mar-07 23:39 
QuestionQuestion about office outlook Pin
ThetaClear17-Mar-07 21:56
ThetaClear17-Mar-07 21:56 
AnswerRe: Question about office outlook Pin
ThetaClear18-Mar-07 5:56
ThetaClear18-Mar-07 5:56 
QuestionHow to get textbox (rich or plain) to remain scrolled to the bottom? Pin
sherifffruitfly17-Mar-07 21:06
sherifffruitfly17-Mar-07 21:06 
AnswerRe: How to get textbox (rich or plain) to remain scrolled to the bottom? Pin
Mudsoad17-Mar-07 21:47
Mudsoad17-Mar-07 21:47 
GeneralRe: How to get textbox (rich or plain) to remain scrolled to the bottom? Pin
sherifffruitfly18-Mar-07 6:42
sherifffruitfly18-Mar-07 6:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.