Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# winform opens with right-click context menu, but how do I get the selected item to show up?

I have a C# WinForm App that I created to store files in a seperate secure location on the hard drive. I am trying to add functionality to the program by adding a right-click context menu so when a user right-clicks a file (or group of files) in windows, my program is there in the context for them to select. No problem there, I have that part worked out. What I need is to programmatically get that list of files and send it to the program so they are listed in the listbox already.

I am already doing something similar with a multiselect in an OFD, but I dont want them to have to open the program, select browse, find the files and select them when they already have them selected in windows.

There are a ton of programs out that have this functionality (like properties plus, textpad, etc...) I just need a shove in the right direction to help me figure this out.

Thanks in advance,

Dave

like image 654
Dave_P Avatar asked Jan 27 '26 16:01

Dave_P


1 Answers

If I'm correctly understanding what you've already implemented, then all the files should appear as arguments on the program's command line. You just need a way of extracting each of those file paths and displaying them in your list view.

In C#, the following code will display a message box containing each argument on the command line:

static void Main(string[] args)
{
    foreach(string arg in args)
    {
        MessageBox.Show(arg);
    }
}

But in case you don't want to access these in the Main method, you can also use the Environment class, which provides the static GetCommandLineArgs method. It returns the same array of strings containing the arguments, and you can loop through it the same way.

like image 182
Cody Gray Avatar answered Jan 30 '26 05:01

Cody Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!