Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取我右键单击的最后一个位置的文件夹路径_C#_Registry_Explorer - Fatal编程技术网

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

C# 获取我右键单击的最后一个位置的文件夹路径,c#,registry,explorer,C#,Registry,Explorer,我为我的应用程序创建了一个右键单击快捷方式条目(在资源管理器的右键单击上下文菜单中),我想获取右键单击位置的文件夹路径,我该怎么做 创建快捷方式的我的代码: RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(@"Directory\Background\shell", true); String[] names = rKey.GetSubKeyNames(); foreach (String s in names) { System.W

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

创建快捷方式的我的代码:

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

提前感谢。

根据您的描述,您的应用程序似乎已在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);
    }
  }
}
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
static void Main(字符串[]参数)//Windows传递一个参数数组,这些参数可能是文件名或文件夹名。
{
字符串avivsfolder=@“\Aviv”;
字符串folderpath=“”;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if(arguments.Length>0)//如果传递了参数。
{
folderpath=参数[0];
尝试
{
if(Directory.Exists(folderpath))//确保文件夹存在。
{
CreateDirectory(folderpath+avivsfolder);
if(Directory.Exists(folderpath+avivsfolder))//检查文件夹是否成功创建,如果没有,异常将停止程序执行,因此不需要使用'else'子句。
{
MessageBox.Show(“指定的文件夹已成功创建。”,“应用程序”,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
其他的
{
抛出新的DirectoryNotFoundException(“指定的文件夹不存在”);
}
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message,“Aviv的应用程序”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else//没有通过任何参数。
{
Show(“您需要选择一个文件夹才能继续。”,“Aviv的应用程序”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
有了这些,我想这就足够完成工作了,如果你需要的话,这就是示例项目


希望对您有所帮助。

您想在哪里捕获文件夹路径?在您的basicFileCreator.exe代码中?我希望在单击该按钮时,该按钮可以创建一个文件或文件夹。(但在我右键单击的同一个文件夹中)首先,感谢您的帮助,但我所看到的是创建一个新文件夹(单击“新建>文件夹”)它工作了,但这不是我想要的,因为每次我点击一个文件夹,它就会打开应用程序。我只希望它出现在上下文菜单中,当我单击它时,它将在同一文件夹中创建一个文件夹。@AvivMamon已更新。我作为示例附加的项目只是为了向您展示,通过快捷方式输入可以实现很多功能。@AvivMamon更新后的代码仅在所选文件夹内创建一个文件夹,如果成功,则显示一个带有消息的MessageBox,顺便说一句,如果需要完全静音,请删除MessageBox语句。