C# Pass file path as argument to main method of my program

Hi experts,
I'm building a winforms program which parses certain xml files. Now I want that when  one of those xml files is double clicked (opened), my program gets called and receives the file path in its main method. I managed to associate xml files to my program, so it opens. But what do I have to do that the file path can be read in my main method? The way I have it below doesn't work. The string array is always empty.

Thanks for your help
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string SelectedFile;            
            if (args.Length > 0)            
            {                
                SelectedFile = Convert.ToString(args[0]);                
                MessageBox.Show(SelectedFile); // Just to confirm it. 
                         
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

Open in new window

arthrexAsked:
Who is Participating?
 
_Katka_Commented:
Did you registered the association as

[HKEY_CLASSES_ROOT\yourExtension\shell\open\command]
@="\"YourExeFolder\" \"%1\""

%1 - meaning a file being opened

regards,
Kate
0
 
_Katka_Commented:
Hi, you have to open Project (in solution explorer, the one you're running) properties, choose debug and fill the parameters line.

regards,
Kate
0
 
_Katka_Commented:
To clarify:

1) right click the project
2) choose Properties
3) choose section debug
4) enter your parameters to a "Command-line arguments"
5) run your project

Screenshot:

http://msmvps.com/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/joacim.metablogapi/6253.image_5F00_thumb_5F00_0A3324F8.png

regards,
Kate
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
arthrexAuthor Commented:
Hmm... I think I don't understand.
The file paths are dynamic.
How can I put something into the command-line arguments?
0
 
_Katka_Commented:
This is just to ensure that your command parsing is working.. i.e. the message box will pop-up. Otherwise it must be compiled into EXE and then called by a different application with parameters like:

my.exe myparameter

this corresponds to command-line arguments value:

myparameter

regards,
Kate
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
For what it's worth, this will do what you want:

in Program.cs, do this:

namespace WindowsFormsApplication2
{
    static class Program
    {
        public static string[] _pubArgs;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main( string[] args )
        {
            _pubArgs = args;

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

In Form1.cs, do this:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            string[] args = WindowsFormsApplication2.Program._pubArgs;

            if ( args.Length > 0 && !string.IsNullOrEmpty( args[ 0 ] ) )
            {
                MessageBox.Show( args[ 0 ] );
            }
        }
    }
}

Yes, it will show the name of the file you clicked on to get the app to run.



0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also use Environment.GetCommandLineArgs():

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs();
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine(i.ToString() + ": " + args[i]);
            }
        }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.