简体   繁体   English

使用右键单击上下文菜单打开C#winform,但如何显示所选项?

[英]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.

相关问题 使用C#为所有桌面快捷方式创建自定义右键单击上下文菜单项(Windows资源管理器) - Create custom right-click context menu item with C# for all desktop shortcuts (windows explorer) ChromiumWebBrowser禁用右键单击上下文菜单C# - ChromiumWebBrowser Disable Right-Click Context Menu C# 无法右键单击上下文菜单条 c# - Can't right-click a context menu strip c# 如何使用 C# 禁用 Windows 中文本框的右键单击上下文菜单? - How to disable the right-click context menu on textboxes in Windows, using C#? 如何在.NET / C#中的右键菜单上绘制? - How to draw on top of the right-click menu in .NET/C#? c#wpf在Windows的右键单击上下文菜单中设置链接 - c# wpf set link in the right-click context menu of windows 枚举文件夹/子文件夹内容,右键单击C#中系统托盘应用程序的上下文菜单 - Enumerate folder/subfolder contents to right-click context menu of system tray application in C# 如何使用 selenium C# 右键单击​​图像并复制图像地址? - How do I right-click on an image and Copy image address using selenium C#? 右键单击上下文菜单后,获取选定的项目 - Get Item selected after right click on context menu 使用C#在Visual Studio中添加右键菜单项吗? - Add a Right-Click Menu Item in Visual Studio using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM