目录右键
Directory right click
我正在设计一个 C# WinForms 程序,当用户右键单击一个目录并选择我添加到 shell 上下文菜单(为我的应用程序打开 .exe)的项目时,它会运行在基于用户右键单击位置的背景中。
我已经弄清楚如何安装它并将其添加到正确的上下文菜单中,但我似乎无法弄清楚该程序最关键的部分。我已经看过 here,但这并没有回答我的问题,它给出的答案只会引出另一个问题。
我也意识到命令行参数的存在,这就是为什么 this question is answered. 当我访问微软网站关于使用命令行参数时,它只是关于使用一个实际的命令行,我没有使用它.
所以我的问题是:
当用户右键单击文件夹并选择我添加的 shell 上下文菜单时,如何准确获取目录路径?
如果我必须在后台使用命令行,那很好,我只需要能够获取目录路径并将其发送到我的程序即可。
这是我如何使用输入目录的相关代码。本质上 source 是用户右键单击时我想要的目录路径。
private void recursiveCheck(string source)
{
string[] directories = Directory.GetDirectories(source);
foreach(string directory in directories)
{
string test = new DirectoryInfo(directory).Name;
if (test.Length >= 3 && (test.Substring(test.Length - 3).Equals("val", StringComparison.InvariantCultureIgnoreCase) || (test.Substring(test.Length - 3).Equals("ash", StringComparison.InvariantCultureIgnoreCase)))
{
if (Directory.Exists(directory + "\STARTUP"))
testing_dir(directory);
else
{
MessageBox.Show("Error! Startup folder does not exist in: " + test);
Application.Exit();
}
}
else
recursiveCheck(directory);
}
}
我假设您已将您的应用程序添加到注册表中文件夹的上下文菜单中:
HKEY_CLASSES_ROOT
Directory
shell
OpenWithMyApp → (Default): Open With My App
command → (Default): "c:\myapp.exe" "%V"
重点在%V
。这将是您右键单击它的文件夹名称,它将作为命令行参数传递给您的应用程序。
然后在你的应用程序中,有这样的东西就足够了:
[STAThread]
static void Main()
{
string folderName = null ;
if (Environment.GetCommandLineArgs().Length > 1)
folderName = Environment.GetCommandLineArgs()[1];
MessageBox.Show(folderName);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
Application.Run(new Form1());
}
我正在设计一个 C# WinForms 程序,当用户右键单击一个目录并选择我添加到 shell 上下文菜单(为我的应用程序打开 .exe)的项目时,它会运行在基于用户右键单击位置的背景中。
我已经弄清楚如何安装它并将其添加到正确的上下文菜单中,但我似乎无法弄清楚该程序最关键的部分。我已经看过 here,但这并没有回答我的问题,它给出的答案只会引出另一个问题。
我也意识到命令行参数的存在,这就是为什么 this question is answered. 当我访问微软网站关于使用命令行参数时,它只是关于使用一个实际的命令行,我没有使用它.
所以我的问题是:
当用户右键单击文件夹并选择我添加的 shell 上下文菜单时,如何准确获取目录路径?
如果我必须在后台使用命令行,那很好,我只需要能够获取目录路径并将其发送到我的程序即可。
这是我如何使用输入目录的相关代码。本质上 source 是用户右键单击时我想要的目录路径。
private void recursiveCheck(string source)
{
string[] directories = Directory.GetDirectories(source);
foreach(string directory in directories)
{
string test = new DirectoryInfo(directory).Name;
if (test.Length >= 3 && (test.Substring(test.Length - 3).Equals("val", StringComparison.InvariantCultureIgnoreCase) || (test.Substring(test.Length - 3).Equals("ash", StringComparison.InvariantCultureIgnoreCase)))
{
if (Directory.Exists(directory + "\STARTUP"))
testing_dir(directory);
else
{
MessageBox.Show("Error! Startup folder does not exist in: " + test);
Application.Exit();
}
}
else
recursiveCheck(directory);
}
}
我假设您已将您的应用程序添加到注册表中文件夹的上下文菜单中:
HKEY_CLASSES_ROOT
Directory
shell
OpenWithMyApp → (Default): Open With My App
command → (Default): "c:\myapp.exe" "%V"
重点在%V
。这将是您右键单击它的文件夹名称,它将作为命令行参数传递给您的应用程序。
然后在你的应用程序中,有这样的东西就足够了:
[STAThread]
static void Main()
{
string folderName = null ;
if (Environment.GetCommandLineArgs().Length > 1)
folderName = Environment.GetCommandLineArgs()[1];
MessageBox.Show(folderName);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
Application.Run(new Form1());
}