在 C# 中更改默认文件夹
Changing Default folder in C#
我正在尝试创建一个控制台应用程序,允许用户访问文件和文件夹的某些属性,例如名称、大小等。这是一项作业,因此要获得奖励分数,我必须将默认文件夹更改为用户仅当用户选择了这样做的选项时才指定文件夹。这里的问题是,在
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);
}
}
谢谢
当你这样做时
Directory.SetCurrentDirectory(userChoiceFolder);
您还应该设置 folderInfo
以使用这个新值。否则,folderInfo
将继续仅提供有关最初设置的 C:\ 文件夹的信息。
此外,由于 files = folderInfo.GetFiles()
是根据 folderInfo 设置的,因此您也需要设置它。最好将这两行移到一个单独的方法中。
我正在尝试创建一个控制台应用程序,允许用户访问文件和文件夹的某些属性,例如名称、大小等。这是一项作业,因此要获得奖励分数,我必须将默认文件夹更改为用户仅当用户选择了这样做的选项时才指定文件夹。这里的问题是,在
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);
}
}
谢谢
当你这样做时
Directory.SetCurrentDirectory(userChoiceFolder);
您还应该设置 folderInfo
以使用这个新值。否则,folderInfo
将继续仅提供有关最初设置的 C:\ 文件夹的信息。
此外,由于 files = folderInfo.GetFiles()
是根据 folderInfo 设置的,因此您也需要设置它。最好将这两行移到一个单独的方法中。