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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_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
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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

_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
_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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.