【问题标题】:Changing Default folder in C#在 C# 中更改默认文件夹
【发布时间】:2015-01-13 20:03:00
【问题描述】:

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

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

上面的 if 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# path directory default


    【解决方案1】:

    当你这样做时

    Directory.SetCurrentDirectory(userChoiceFolder);
    

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

    另外,由于files = folderInfo.GetFiles() 是根据 folderInfo 设置的,因此您也需要设置它。最好将这两行移到单独的方法中。

    【讨论】:

    • @susheeshix 我正在尝试这样做,但它会出现错误
    • 您是如何尝试更改值的?您需要像构建第一个对象一样使用新的构造函数来构建新对象。
    • @sudheeshix 可能是我做错了,就像这样 folderInfo = Directory.SetDirectory(userChoiceFolder);
    • @BenKnoble 我试过了,但它不适用于整个程序,它就像块中发生的任何事情都留在块中......我也是一个初学者,所以这就是为什么我正在努力解决这个问题
    • 查看SetCurrentDirectory的返回类型。它是无效的。所以,你不能用它来重置folderInfo。使用userChoiceFolder 重新设置。 folderInfo = new DirectoryInfo(userChoiceFolder);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2016-05-30
    • 2023-03-23
    • 2012-11-14
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多