Question

I am creating a C# utility that will edit a document when the user right clicks on a file and selects mine from the menu. My question is how do I get a string of the filename to the program so that it can edit it?

Was it helpful?

Solution

The arguments you send to your program are sent as the String[] array in the Main function of your program. These are called Command-Line Arguments. If you know how to use a String array, you know how to use them.

    static void Main(string[] args)
    {
        foreach (var arg in args)
        {
            Console.WriteLine(arg);
        }
    }

By the way, to add your program to the context menu of the file, you need to modify the registry. If you search online, you'll find enough tutorials and articles about this.

OTHER TIPS

Just in case you are asking for a windows app. You can do the same thing as with a console app:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        string myvalue = args[0]; //get first value in arguments
        //do things with my value here

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1(myvalue));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top