从 Windows 上下文菜单接收参数

发布于 2024-08-17 18:40:32 字数 169 浏览 6 评论 0原文

我以前曾经这样做过,但我一生都不记得如何做到这一点...

在我的资源管理器上下文菜单中,我添加了一个新条目(转到regedit...转到HKEY_CLASSES_ROOT...bla bla bla)。 .. 现在,当我单击我的选项时,我想将文件路径、文件名等传递给我的应用程序......然后在那里使用它?

I have done this before but for the life of me can't remember how to do this...

In my explorer context menu I added a new entry (go to regedit...go to HKEY_CLASSES_ROOT...bla bla bla)... Now when I click on my option I want to pass in the file path, file name, those kind of things to my application...and then use it there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七禾 2024-08-24 18:40:32

.ext\shell\open\command 键的默认值应包含带有“%1”参数的 .exe 路径。资源管理器将其替换为文件的完整路径。您可以通过 Main() 方法参数或 Environment.GetCommandLineArgs() 在 .exe 中读取它。

The default value of the .ext\shell\open\command key should contain the path to your .exe with the "%1" argument. Explorer substitutes that with the full path to the file. Which you can read in your .exe through the Main() method argument or Environment.GetCommandLineArgs().

海风掠过北极光 2024-08-24 18:40:32

尝试这个项目链接:
如何在单击上下文菜单项时获取值并传递与可执行文件的参数相同
.NET Shell 扩展 - Shell 上下文菜单

或这个网址:codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser


代码示例:

    // Variables
    private StringDictionary Parameters;

    // Constructor
    public Arguments(string[] Args)
    {
        Parameters = new StringDictionary();
        Regex Spliter = new Regex(@"^-{1,2}|^/|=|:",
            RegexOptions.IgnoreCase|RegexOptions.Compiled);

        Regex Remover = new Regex(@"^['""]?(.*?)['""]?$",
            RegexOptions.IgnoreCase|RegexOptions.Compiled);

        string Parameter = null;
        string[] Parts;

        // Valid parameters forms:
        // {-,/,--}param{ ,=,:}((",')value(",'))
        // Examples: 
        // -param1 value1 --param2 /param3:"Test-:-work" 
        //   /param4=happy -param5 '--=nice=--'
        foreach(string Txt in Args)
        {
            // Look for new parameters (-,/ or --) and a
            // possible enclosed value (=,:)
            Parts = Spliter.Split(Txt,3);

            switch(Parts.Length){
            // Found a value (for the last parameter 
            // found (space separator))
            case 1:
                if(Parameter != null)
                {
                    if(!Parameters.ContainsKey(Parameter)) 
                    {
                        Parts[0] = 
                            Remover.Replace(Parts[0], "$1");

                        Parameters.Add(Parameter, Parts[0]);
                    }
                    Parameter=null;
                }
                // else Error: no parameter waiting for a value (skipped)
                break;

            // Found just a parameter
            case 2:
                // The last parameter is still waiting. 
                // With no value, set it to true.
                if(Parameter!=null)
                {
                    if(!Parameters.ContainsKey(Parameter)) 
                        Parameters.Add(Parameter, "true");
                }
                Parameter=Parts[1];
                break;

            // Parameter with enclosed value
            case 3:
                // The last parameter is still waiting. 
                // With no value, set it to true.
                if(Parameter != null)
                {
                    if(!Parameters.ContainsKey(Parameter)) 
                        Parameters.Add(Parameter, "true");
                }

                Parameter = Parts[1];

                // Remove possible enclosing characters (",')
                if(!Parameters.ContainsKey(Parameter))
                {
                    Parts[2] = Remover.Replace(Parts[2], "$1");
                    Parameters.Add(Parameter, Parts[2]);
                }

                Parameter=null;
                break;
            }
        }
        // In case a parameter is still waiting
        if(Parameter != null)
        {
            if(!Parameters.ContainsKey(Parameter)) 
                Parameters.Add(Parameter, "true");
        }
    }

    // Retrieve a parameter value if it exists 
    // (overriding C# indexer property)
    public string this [string Param]
    {
        get
        {
            return(Parameters[Param]);
        }
    }
}

}

Try this projects links:
How to fetch the value on the click of Context Menu Item and pass the same as a parameter to the executable ,
.NET Shell Extensions - Shell Context Menus

or this one this url: codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser


code exemple:

    // Variables
    private StringDictionary Parameters;

    // Constructor
    public Arguments(string[] Args)
    {
        Parameters = new StringDictionary();
        Regex Spliter = new Regex(@"^-{1,2}|^/|=|:",
            RegexOptions.IgnoreCase|RegexOptions.Compiled);

        Regex Remover = new Regex(@"^['""]?(.*?)['""]?$",
            RegexOptions.IgnoreCase|RegexOptions.Compiled);

        string Parameter = null;
        string[] Parts;

        // Valid parameters forms:
        // {-,/,--}param{ ,=,:}((",')value(",'))
        // Examples: 
        // -param1 value1 --param2 /param3:"Test-:-work" 
        //   /param4=happy -param5 '--=nice=--'
        foreach(string Txt in Args)
        {
            // Look for new parameters (-,/ or --) and a
            // possible enclosed value (=,:)
            Parts = Spliter.Split(Txt,3);

            switch(Parts.Length){
            // Found a value (for the last parameter 
            // found (space separator))
            case 1:
                if(Parameter != null)
                {
                    if(!Parameters.ContainsKey(Parameter)) 
                    {
                        Parts[0] = 
                            Remover.Replace(Parts[0], "$1");

                        Parameters.Add(Parameter, Parts[0]);
                    }
                    Parameter=null;
                }
                // else Error: no parameter waiting for a value (skipped)
                break;

            // Found just a parameter
            case 2:
                // The last parameter is still waiting. 
                // With no value, set it to true.
                if(Parameter!=null)
                {
                    if(!Parameters.ContainsKey(Parameter)) 
                        Parameters.Add(Parameter, "true");
                }
                Parameter=Parts[1];
                break;

            // Parameter with enclosed value
            case 3:
                // The last parameter is still waiting. 
                // With no value, set it to true.
                if(Parameter != null)
                {
                    if(!Parameters.ContainsKey(Parameter)) 
                        Parameters.Add(Parameter, "true");
                }

                Parameter = Parts[1];

                // Remove possible enclosing characters (",')
                if(!Parameters.ContainsKey(Parameter))
                {
                    Parts[2] = Remover.Replace(Parts[2], "$1");
                    Parameters.Add(Parameter, Parts[2]);
                }

                Parameter=null;
                break;
            }
        }
        // In case a parameter is still waiting
        if(Parameter != null)
        {
            if(!Parameters.ContainsKey(Parameter)) 
                Parameters.Add(Parameter, "true");
        }
    }

    // Retrieve a parameter value if it exists 
    // (overriding C# indexer property)
    public string this [string Param]
    {
        get
        {
            return(Parameters[Param]);
        }
    }
}

}

一人独醉 2024-08-24 18:40:32

您应该从上下文菜单中打开一个窗口,然后将数据传递到您的应用程序。

You should open a window from the context menu and get your data there to pass to your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文