在C#中更改默认文件夹

时间:2015-01-13 20:03:01

标签: c# path directory default

我正在尝试创建一个控制台应用程序,允许用户访问文件和文件夹的某些属性,如名称,大小等。这是一项任务,所以要获得奖励标记,我必须将默认文件夹更改为用户指定的文件夹仅当用户选择这样做的选项时。这里的问题是,

if else (userSelection == 2) // code to allow users to change folder path
    {
         Console.WriteLine("Enter the Path of the Folder you wish to keep as default");
         userChoiceFolder = Console.ReadLine();

         if (Directory.Exists(userChoiceFolder))
         {
             Directory.SetCurrentDirectory(userChoiceFolder);
             Console.WriteLine(Directory.GetCurrentDirectory());
         }
    }

如果上面的else语句,它确实会更改文件夹,但仅限于if else范围内。 我想要做的是更改

中的默认路径
DirectoryInfo folderInfo = new DirectoryInfo("C:\\");

用户在if else选择2语句中指定的内容。那么用户指定的内容将替换“C:\”)。这是完整的代码

        DirectoryInfo folderInfo = new DirectoryInfo("C:\\");
        FileInfo[] files = folderInfo.GetFiles();
        int userSelection;
        string userInput;
        string userChoiceFolder;

        Console.WriteLine("Welcome! Please make a selection by Entering 1, 2, 2 or 4");
        DisplayMenuOptions();
        bool isUserSelection = int.TryParse(Console.ReadLine(), out userSelection);

        while (isUserSelection == false || userSelection >= 1 && userSelection <= 5)
        {
            if (userSelection == 1)
            {
                Console.WriteLine("Files in C/: {0}", folderInfo.Name);

                for (int index = 0; index < files.Length; index++)
                {
                    //Code for file info
                }
            }
            else if (userSelection == 2)
            {
                Console.WriteLine("Enter the Path of the Folder you wish to keep as default");
                userChoiceFolder = Console.ReadLine();

                if (Directory.Exists(userChoiceFolder))
                {
                    Directory.SetCurrentDirectory(userChoiceFolder);
                    Console.WriteLine(Directory.GetCurrentDirectory());
                }
            }
            else if (userSelection == 3)
            {
                //Code for filtered file listing
                for (int fIndex = 0; fIndex < fileType.Length; fIndex++)
                {
                    //Filetered fie listing display
                }
            }
            else if (userSelection == 4)
            {
                //File statitstics code
            }
            else if (userSelection == 5)
            {
                return;
            }

            else
            {
                Console.WriteLine("Sorry, to make a selection you must enter 1, 2, 3 or 4");
            }

            Console.WriteLine("Press any key to Continue");
            Console.ReadKey();
            Console.Clear();
            DisplayMenuOptions();
            bool isSelection = int.TryParse(Console.ReadLine(), out userSelection);

        }
    }

谢谢

1 个答案:

答案 0 :(得分:1)

当你这样做时

Directory.SetCurrentDirectory(userChoiceFolder);

您还应将folderInfo设置为使用此新值。否则,folderInfo将继续仅提供有关最初设置的C:\文件夹的信息。

此外,由于files = folderInfo.GetFiles()是基于folderInfo设置的,因此您还需要设置它。最好将这两行移动到一个单独的方法中。