Farha Abdul
Farha Abdul

Reputation: 33

Setting folder to user specified path in C#

I am trying to set the current folder of my console application to a user specified path in C# but I can't. I am new to programming and C# is my first language. Here is the code so far, I don't know where I am going wrong I have searched the internet for this, followed the steps but it does not set the folder to what the user specifies. What I am trying to do here is to change the folder path to what the user wants to, and set it as the current folder from which the user can then access its files.

   DirectoryInfo folderInfo = new DirectoryInfo("C:\\Windows");
   FileInfo[] files = folderInfo.GetFiles();

   Console.WriteLine("Enter Folder Name");
   string userInput = Console.ReadLine();
   FileInfo[] fileType = folderInfo.GetFiles(userInput + "*" + ".", SearchOption.TopDirectoryOnly); //searches for the folder the user has specified 
   Directory.SetCurrentDirectory(userInput);

   Console.WriteLine("{0}", userInput );
   Console.ReadLine();

The Error I get is

"An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll"

Additional information: Could not find a part of the path 'folder name here'.

Please bear in mind I am a beginner at this. Thank you in advance

Upvotes: 1

Views: 5169

Answers (2)

Steve
Steve

Reputation: 216363

When you call DirectoryInfo.GetFiles, the first parameter is a file pattern (like *.* or *.txt), but you could also specify a subfolder of the referenced folder. However you need to respect the syntax rules for specifying folders. The best approach to create folder names is through the various static methods of the path class

DirectoryInfo folderInfo = new DirectoryInfo("C:\\Windows");
Console.WriteLine("Enter Folder Name");
string userInput = Console.ReadLine();
string subFolder = Path.Combine(folderInfo.FullName, userInput);

// Check to verify if the user input is valid
if(Directory.Exists(subFolder))
{
    FileInfo[] fileType = folderInfo.GetFiles(Path.Combine(userInput, "*.*"), 
                                     SearchOption.TopDirectoryOnly);
    Directory.SetCurrentDirectory(subFolder);
    Console.WriteLine("{0}", Directory.GetCurrentDirectory());
}
else 
    Console.WriteLine("{0} doesn't exist", subFolder);

A part from the problems with DirectoryInfo paths you should also consider that SetCurrentDirectory could use a relative path but it is considered to be relative to the CurrentDirectory and not to initial the C:\WINDOWS (unless C:\WINDOWS is the current working directory), so if you can provide a full path to SetCurrentDirectory you are safer.

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61379

You absolutely need the full path for the "Directory" to work.

In other words, it will never find "Users" or "Windows", it has to be "C:\Windows". Relative pathing will work, but its relative to your current working directory (the application directory).

If you expect that to work, you need to run:

Directory.SetCurrentDirectory("C:\\");

Before running your file search, so the relative path will evaluate correctly. Also, your search pattern is messed up, you don't include the user input for that, as @Steve mentioned.

Upvotes: 0

Related Questions