C# 上下文Shell菜单-单击获取标识符

C# 上下文Shell菜单-单击获取标识符,c#,winforms,windows-shell,C#,Winforms,Windows Shell,我已经为/on.txt文件创建了一个上下文shell菜单 其“操作”类似于“使用记事本编辑”选项 我可以使用以下代码单击菜单打开“记事本”—— subKey.SetValue("", "C:\\Windows\\System32\\notepad.exe"); //subKey is the newly created sub key - The key creation part works fine. 如何使用类似于“使用记事本编辑”功能的功能?或者至少可以获取触发此事件的“.txt”

我已经为/on
.txt
文件创建了一个上下文shell菜单

其“操作”类似于“使用记事本编辑”选项

我可以使用以下代码单击菜单打开“记事本”——

subKey.SetValue("", "C:\\Windows\\System32\\notepad.exe");

 //subKey is the newly created sub key - The key creation part works fine.
如何使用类似于“使用记事本编辑”功能的功能?或者至少可以获取触发此事件的“.txt”文件的名称吗

注意:通过“使用记事本编辑”,我的意思是在记事本中查看所选文件的内容。

shell(
explorer.exe
)将用文件名替换
%1
。所以你基本上写:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\txtfile\shell\openwithnotepad]
@="Open with &Notepad"

[HKEY_CLASSES_ROOT\txtfile\shell\openwithnotepad\command]
@="\"C:\\Windows\\System32\\notepad.exe\" \"%1\""
文件名将作为命令行参数传递给
C:\Windows\System32\notepad.exe
。例如,如果打开
D:\blah.txt
,记事本将收到
D:\blah.txt
作为第一个参数

在C#中,基本上使用或来检索文件路径

例如:

string[] commandLineArgs = Environment.GetCommandLineArgs();
string fileToOpen = null;
if (commandLineArgs.Length > 1)
{
    if (File.Exists(commandLineArgs[1]))
    {
        fileToOpen = commandLineArgs[1];
    }
}
if (fileToOpen == null)
{
    // new file
}
else
{
    MyEditor.OpenFile(fileToOpen);
}
shell(
explorer.exe
)将用文件名替换
%1
。所以你基本上写:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\txtfile\shell\openwithnotepad]
@="Open with &Notepad"

[HKEY_CLASSES_ROOT\txtfile\shell\openwithnotepad\command]
@="\"C:\\Windows\\System32\\notepad.exe\" \"%1\""
文件名将作为命令行参数传递给
C:\Windows\System32\notepad.exe
。例如,如果打开
D:\blah.txt
,记事本将收到
D:\blah.txt
作为第一个参数

在C#中,基本上使用或来检索文件路径

例如:

string[] commandLineArgs = Environment.GetCommandLineArgs();
string fileToOpen = null;
if (commandLineArgs.Length > 1)
{
    if (File.Exists(commandLineArgs[1]))
    {
        fileToOpen = commandLineArgs[1];
    }
}
if (fileToOpen == null)
{
    // new file
}
else
{
    MyEditor.OpenFile(fileToOpen);
}

据我所知,您需要创建一个“资源管理器扩展”来创建自定义上下文菜单。我可以通过访问注册表来创建菜单或“菜单项”(对不起,我指的是“用..打开”类型),也可以打开记事本,但可以作为“创建新文件”类型。您如何创建菜单?请检查此项,这可能会有帮助:据我所知,您需要创建一个“资源管理器扩展名”,以便能够创建自定义上下文菜单。我可以通过访问注册表来创建菜单或“菜单项”(对不起,我指的是“用..打开”类),并且可以打开记事本,但作为“创建新文件”类型。您如何创建菜单?请检查此项,这可能会有所帮助:我以“管理员模式”运行注册表。正在创建注册表项,但我在右键单击.txt文件时无法看到相同的内容。执行此操作后,它表示已成功将注册表项添加到注册表中。不过,我没有看到
openwithnotepad
选项。请尝试手动将
HKCR\txtfile\shell\openwithnotepad\command
的默认值编辑为
“C:\Windows\System32\notepad.exe”“%1”
。这是当前设置的值。让我们以“管理员模式”运行注册表。正在创建注册表项,但我在右键单击.txt文件时无法看到相同的内容。执行此操作后,它表示已成功将注册表项添加到注册表中。不过,我没有看到
openwithnotepad
选项。请尝试手动将
HKCR\txtfile\shell\openwithnotepad\command
的默认值编辑为
“C:\Windows\System32\notepad.exe”“%1”
。这是当前设置的值。让我们来看看