从上下文菜单

时间:2018-02-23 09:42:34

标签: c#

  1. 我想从上下文菜单而不是一个文件夹创建一组文件夹。

  2. 我已经创建了一个可以执行此操作的批处理脚本并将其放在上下文菜单中。

  3. 我还创建了一个小C#控制台应用程序来执行相同操作,但问题是脚本/应用程序只能在脚本/应用程序中手动设置的特定位置创建文件夹。基本上我不能在任何地方创建文件夹,只能在我在脚本/应用程序中设置的位置。

  4. 问题:

    有没有办法获取调用上下文菜单的当前OPEN目录并传递给控制台应用程序?

    如果有意义的话,我想从上下文菜单中在当前打开的目录中创建一组文件夹。

    提前致谢。

    更新:这是我最终提出的,正是我所需要的(虽然,它可能是蹩脚的实现,因为这是我的第一个控制台应用程序,但它工作大声笑):

    class CreateFolders
    {
        public static int countItem = 0;
    
        public static void Main()
        {
            try
            {       
                string path = Directory.GetCurrentDirectory ();
                string originalPath = Directory.GetCurrentDirectory ();
                string filePath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\CreateFolders\\folders.txt";
                string[] folders ={};
                string line;
                int counter = 0;
    
                StreamReader file = new StreamReader (filePath);
    
                if(new FileInfo(filePath).Length != 0)
    
                {
                    List<string> folderNames = new List<string>();
                    while ((line = file.ReadLine ()) != null)
                    {
                        folderNames.Add (line);
                        counter++;
                    }
    
                    folders = folderNames.ToArray();
                    int arrayLength = folders.Length;
    
                    for (int i = 0; i < arrayLength; i++)
    
                        {
                            CreateFolder (path += "\\" + folders[i]);
                            path = originalPath;
                        }
    
                    if (countItem != 0)
                    {
                        Console.WriteLine ("\nGreat! {1} folders have been created in: {0}\n", originalPath, countItem);
                    }
    
                } else
                    {
                    Console.WriteLine ("File is empty. Add names of your folders\n");
                    }
    
                Console.WriteLine ("\n");
                Console.ReadKey();
            }
    
            catch (Exception ex)
            {
                Console.WriteLine("Folders were not created :(\n");
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    
        static void CreateFolder(string path)
        {
            DirectoryInfo folder = new DirectoryInfo(path);
            string dirName = folder.Name;
    
            if (!Directory.Exists (path))
            {
                Directory.CreateDirectory (path);
                path = " ";
                Console.WriteLine ("{0} ok", dirName);
                countItem++;
            }
            else
            {
                Console.WriteLine ("{0} exists", dirName);
            }
        }
    
    }
    

0 个答案:

没有答案
相关问题