0

Is there any way to detect when a file is opened with a c# application

Example When I right-click on a file then select to open it with My Application I want this code to run

File.WriteLines(OpenWithFile)

Is this achievable

MeferGota
  • 31
  • 4
  • If you right-click a file and select _Open With_ and then your application, your app will see it the same way it would if someone typed `YourApp.exe That.File` at the command line – Flydog57 Sep 18 '21 at 23:47
  • Windows passes it to your application as a command-line parameter. You can search this site for `[c#] get command line parameter` to find examples of how you can get it. – Ken White Sep 18 '21 at 23:56
  • I'm kinda new to c#. can you explain how to get the command line parameter? – MeferGota Sep 19 '21 at 00:34
  • Does this answer your question? [How to set C# application to accept Command Line Parameters?](https://stackoverflow.com/questions/42535826/how-to-set-c-sharp-application-to-accept-command-line-parameters) – Charlieface Sep 19 '21 at 01:17

1 Answers1

0

Yes, it's achievable. When a user clicks Open With in Windows Explorer, the operating system includes the filename as the first argument. Below is a demo how you'd get the file name that the user clicked to open with your application. You can than implement whatever processing you wish.

 class Program
{
    static void Main(string[] args)
    {
        if (args.Length > 0) 
        {
            var OpenWithFile = args[0];
            Console.WriteLine($"The OpenWithFile is: {OpenWithFile}");
        }
        else
        {
            Console.WriteLine("No command-line arguments were passed.");
        }
        Console.WriteLine("Press any key to continue.");
        Console.ReadLine(); 
    }
}
jjthebig1
  • 629
  • 7
  • 12