获取最后一个位置的文件夹路径,我右键单击

时间:2013-09-14 10:45:53

标签: c# registry explorer

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

我创建快捷方式的代码:

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();   

提前致谢。

1 个答案:

答案 0 :(得分: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);
    }
  }
}

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

希望它对你有所帮助。