从右键单击上下文菜单中获取文件夹的路径 [英] Geting the path of a folder from the right click context menu

查看:91
本文介绍了从右键单击上下文菜单中获取文件夹的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我试图启动一个C#应用程序,只是在Windows中形成一个右键单击上下文菜单,当该应用程序打开时,我想用执行该操作的文件夹的路径填充一个TextBox,如图所示:
https://plus.google.com/u/0/photos/105606022912012406932/albums/5670323163492912417/5670323163285239298

我不想在文本框中为文件夹的路径编写manuale,以完成自动操作.

谢谢!

Hi!

I''m trying to start an c# application just form a right click context menu in windows and when the application will open i want to fill a TextBox with the path of the folder I made that action, like in the picture:
https://plus.google.com/u/0/photos/105606022912012406932/albums/5670323163492912417/5670323163285239298

I want not to write manuale in the textbox the path of the folder, to complete automatic.

Thanks!

推荐答案

好,所以这是我在注册表中查看VLC媒体播放器是如何做到的.

首先,您将需要创建一个注册表项.如果希望上下文菜单项对当前用户可用,则仅使用Registry.CurrentUser作为根密钥,否则,如果希望上下文菜单项对所有用户可用,则将Registry.LocalMachine作为根密钥.好的,这是添加注册表项的方法:

Ok so here is what I found out by looking in the registry at how VLC media player does this.

First you will need to create a registry key. If you want the context menu entry to be available for the current user only use Registry.CurrentUser as the root key else if you want the context menu entry to be available for all users use Registry.LocalMachine as the root key. Ok so here is how to add the registry entry:

RegistryKey rootKey = Registry.CurrentUser;
RegistryKey subKey = rootKey.CreateSubKey("Software\\Classes\\Directory\\shell\\OpenWithMyApp");
subKey.SetValue("", "Open With My App");
subKey.CreateSubKey("command").SetValue("", Application.ExecutablePath + " %1");
subKey.Close();
rootKey.Close();



现在可以在您的应用程序中检索目录路径:

在您的应用程序Program.cs文件中,从
更改您的主要方法



Ok now to retrieve the directory path in your application:

In your applications Program.cs file change your main method from

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}







to

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1(args));
}



您的应用程序主窗体中的
会修改您的构造函数



the in your applications main form modify your constructor from

public Form1()
{
}







to

public Form1(string[] args)
{
    string folderPath = args[0];
}



至于用文件夹路径填充文本框,只需将文件夹路径分配给文本框即可.例如:



And as far as populating the text box with the folder path just assign folder path to the text box. For example:

textBox1.Text = folderPath;



注意:如果将此应用程序部署为clickonce应用程序,则需要使用:



Note: if this app will be deployed as a clickonce app you will need to use:

string[] args = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;


而不是string [] args来检索您的应用参数.

当然可以对其进行修改以适合您.

另外,添加注册表项的最佳方法是使用安装项目,该方法将在安装应用程序时添加条目,而在卸载应用程序时删除.

希望对您(或某人)有帮助.请发表评论,让我知道它对您有什么帮助.


instead of string[] args to retrieve your apps arguments.

And of course modify it to work for you.

Also the best way to add the registry entry is through the use of a setup project that way the entry will be added when the app is installed and removed when the app is uninstalled.

I hope this will help you (or someone) Please post a comment and let me know how it worked out for you.


这篇关于从右键单击上下文菜单中获取文件夹的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆