在 C# 中更改默认文件夹

本文关键字:默认 文件夹 | 更新日期: 2023-09-27 18:37:21

我正在尝试创建一个控制台应用程序,允许用户访问文件和文件夹的某些属性,例如名称,大小等。这是一项分配,因此要获得奖励分数,我必须仅在用户选择选项时才将默认文件夹更改为用户指定的文件夹。这里的问题是,在

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

谢谢

在 C# 中更改默认文件夹

当你这样做时

Directory.SetCurrentDirectory(userChoiceFolder);

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

此外,由于files = folderInfo.GetFiles()是根据文件夹信息设置的,因此您也需要进行设置。最好将这 2 行移动到单独的方法中。