0

我为我的应用程序创建了一个右键单击快捷方式条目(在资源管理器的右键单击上下文菜单中),我想获取右键单击位置的文件夹路径,我该怎么做?

我创建快捷方式的代码:

RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(@"Directory\Background\shell", true);
String[] names = rKey.GetSubKeyNames();
foreach (String s in names)
{
    System.Windows.Forms.MessageBox.Show(s);
}
RegistryKey newKey = rKey.CreateSubKey("Create HTML Folder");
RegistryKey newSubKey = newKey.CreateSubKey("command");
newSubKey.SetValue("", @"C:\Users\Aviv\Desktop\basicFileCreator.exe " + "\"" + "%1" + "\"");
newSubKey.Close();
newKey.Close();
rKey.Close();   

提前致谢。

4

1 回答 1

1

从您的描述看来,您有一个在 Windows 资源管理器的上下文菜单中注册的应用程序,您需要的是右键单击的文件夹路径。
好吧,如果是这种情况,那么我想告诉你,它不会像你期望的那样工作。

为此特定目的,您需要以下密钥而不是您的:

RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(@"Directory\shell", true);
String[] names = rKey.GetSubKeyNames();
foreach (String s in names)
{
    System.Windows.Forms.MessageBox.Show(s);
}
RegistryKey newKey = rKey.CreateSubKey("Create HTML Folder");
RegistryKey newSubKey = newKey.CreateSubKey("command");
newSubKey.SetValue("", @"C:\Users\Aviv\Desktop\basicFileCreator.exe " + "\"" + "%1" + "\"");
newSubKey.Close();
newKey.Close();
rKey.Close();   

完成后,现在我们准备在我们的应用程序中实现此功能。
为此,请将以下代码添加到解决方案的Program.cs文件中:

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] arguments)//Windows passes an array of arguments which may be filesnames or folder names.
{
    string avivsfolder = @"\Aviv";
    string folderpath = "";

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (arguments.Length > 0)//If an argument has been passed.
    {
        folderpath = arguments[0];
        try
        {
            if (Directory.Exists(folderpath))//Make sure the folder exists.
            {
                Directory.CreateDirectory(folderpath + avivsfolder);

                if (Directory.Exists(folderpath + avivsfolder))//To check if the folder was made successfully,if not an exception would stop program exceution,thus no need for 'else' clause.
                {
                    MessageBox.Show("The specified folder was created successfully.", "Application", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }

            else
            {
                throw new DirectoryNotFoundException("The specified folder does not exist");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Aviv's Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    else//No argument passed.
    {
        MessageBox.Show("You need to select a folder to continue.", "Aviv's Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
  }
}

有了这个,我想这足以完成工作,如果你需要,这里是示例项目。

希望它可以帮助你。

于 2013-09-14T14:19:13.063 回答