[英]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. 我有一个C#WinForm应用程序,我创建用于将文件存储在硬盘驱动器上的单独安全位置。 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.
我试图通过添加右键单击上下文菜单为程序添加功能,因此当用户右键单击Windows中的文件(或文件组)时,我的程序就在上下文中供他们选择。 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. 我已经在OFD中做了类似于多选的东西,但是我不希望他们必须打开程序,选择浏览,找到文件并在他们已经在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 戴夫
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: 在C#中,以下代码将在命令行上显示一个包含每个参数的消息框:
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 . 但是如果您不想在
Main
方法中访问它们,您还可以使用Environment
类,它提供静态GetCommandLineArgs
方法 。 It returns the same array of strings containing the arguments, and you can loop through it the same way. 它返回包含参数的相同字符串数组,您可以以相同的方式循环遍历它。
Here is an article on how to customise Right-Click Menu Options in Windows 这篇文章介绍了如何在Windows中自定义右键菜单选项
Then just as #CodyGray says use the string[] args
in your Main
method of you program to get the filenames 然后正如#CodyGray所说,在你的程序的
Main
方法中使用string[] args
来获取文件名
I am gathering all the arguments and sending them to an ArrayList. 我正在收集所有参数并将它们发送到ArrayList。
static void Main(string[] args)
{
ArrayList myAL = new ArrayList();
foreach (string arg in args)
{
myAL.Add(arg);
}
ALRec nalr = new ALRec();
nalr.getArrList(myAL);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
sending it to ALRec Class 将其发送到ALRec Class
class ALRec
{
ArrayList MyArrLst = new ArrayList();
public void getArrList(ArrayList AL)
{
MyArrLst = AL;
}
}
Why is it starting multiple instances of my App? 为什么它会启动我的应用程序的多个实例?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.