C#:右键单击文件夹名称时获取文件夹名称
我正在开发一个Windows应用程序,我需要在右键单击文件夹对其进行一些操作时获取文件夹名称。
到目前为止,我做了以下操作:
- 在 HKKEY_CLASS_ROOT\Folder\shell\(我的程序名称) 中创建了一个注册表子
- 项 在我的程序名称\命令 [我的程序的路径] 中创建了一个注册表子项
现在我使注册表项显示在文件夹上下文菜单。在我的应用程序中,我执行了以下操作:
1-在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-在我的form1中:
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 ,如果 args.length > 这是我的问题0 它只显示长度为 0 的路径而不是完整路径。
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 :
- Made a registry subkey in HKKEY_CLASS_ROOT\Folder\shell\(my program name)
- Made a registry subkey of my program name\command [the path of my program]
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
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- in my form1 :
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;
}
}
finally :
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将注册表中的
%0
用引号括起来,以强制将整个路径视为单个参数。否则,空格将被视为参数分隔符。
您还可以调用 String.Join(" ", args) 来手动重新组合所有参数,但第一种方法更好。
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.
You could also call
String.Join(" ", args)
to manually recombine all of the arguments, but the first way is better.