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



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

我创建快捷方式的代码:

RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(@"DirectoryBackgroundshell", 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:UsersAvivDesktopbasicFileCreator.exe " + """ + "%1" + """);
newSubKey.Close();
newKey.Close();
rKey.Close();   

提前谢谢。

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

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

RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(@"Directoryshell", 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:UsersAvivDesktopbasicFileCreator.exe " + """ + "%1" + """);
newSubKey.Close();
newKey.Close();
rKey.Close();   

完成此操作后,现在我们已准备好在应用程序中实现此功能。
为此,请将以下代码添加到解决方案的程序.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);
    }
  }
}

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

希望对您有所帮助。

最新更新