문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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));
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top