C#:右键单击获取文件夹名称 [英] C# : getting folder name when right click on it

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

问题描述

我正在开发Windows应用程序,我需要在右键单击Folder的同时获取Folder名称,以对其进行一些操作。

I am developing a windows application, I need to get the Folder name while right clicking on the Folder to do some operations on it .

到目前为止,我已经完成了以下:

So far I did the following :


  1. 在HKKEY_CLASS_ROOT\文件夹\shell\(我的程序名称)中创建注册表子项

  2. 制作我的程序名称的注册表子项\命令[我的程序的路径]

现在我做了注册表键显示在文件夹上下文菜单中。在我的应用程序中,我执行了以下操作:

now I made the registry key to be displayed in folder context menu. And in my application I did the following :

1- in program.cs

1- in program.cs

    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 p = new Form1();
        if (args.Length > 0)
        {
            p.pathkey = args[0];
        }
        Application.Run(p);
    }

2-以我的形式1:

    private string _pathkey;
    public string pathkey
    {
        get { return _pathkey; }
        set { _pathkey = value; }
    }

    private void Form1_Load(object sender, EventArgs e)
    { 
        if (this.pathkey != null)
        {
           textBox1.Text=pathkey;
        }
    }

最后:

现在,当我右键单击一个文件夹时,可以说例如一个名为NEW的文件夹。然后textbox3.text = C:\NEW,到目前为止一切正常,但是如果文件夹名称是New Folder,那么textbox3.text = C:\New仅不是C:\New Folder,这就是我的问题。 length> 0只会显示长度为0而不是完整路径。

now when I right click on a folder lets say for example called NEW. then textbox3.text = C:\NEW , so far it works fine but if the folder name is New Folder then textbox3.text = C:\New only not C:\New Folder and that is my problem if args.length > 0 it does only display the the lenght 0 not the full path.

推荐答案

您需要将<$ c $注册表中的c>%0 用引号引起来,将整个路径强制视为单个参数。

否则,空格被视为参数分隔符。

You need to put the %0 in the registry in quotes to force the entire path to be treated as a single argument.
Otherwise, the spaces are treated as argument separators.

您也可以调用 String.Join(,args)手动重新组合所有参数,但是第一种方法是更好。

You could also call String.Join(" ", args) to manually recombine all of the arguments, but the first way is better.

这篇关于C#:右键单击获取文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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