This is a continuation of a previous problem I had with a "dir" like program. The previous solution actually *doesn't* work as I'd hoped. the Path class works, as far as I can tell, when the paramater is passed in a very specific way. It doesn't work if I don't have a specific volume, directory, and filename listed. For example, if I just use "..\*.exe", it will recognize that the "directory" is .., and the filename is *.exe... but it will not infer the volume since it is not listed in the paramater.
This is my code so far.
static void Main(string[] args) {
string exeName = Environment.CommandLine.Remove(Environment.CommandLine.IndexOf(".exe"));
string workingDirectory = "";
string fileSearchString = "";
DirectoryInfo[] dirList;
FileInfo[] fileList;
switch (args.Length) {
case 0:
workingDirectory = Environment.CurrentDirectory;
fileSearchString = "*.*";
break;
case 1:
workingDirectory = args[0];
break;
default:
Console.WriteLine("The syntax of the command is incorrect.");
Console.WriteLine("Usage: {0} [drive:][path][filename]", exeName);
return;
}
DirectoryInfo currentDir = new DirectoryInfo(workingDirectory);
dirList = currentDir.GetDirectories();
fileList = currentDir.GetFiles(fileSearchString);
foreach (DirectoryInfo dir in dirList)
Console.WriteLine("Directory: {0}", dir);
foreach (FileInfo file in fileList)
Console.WriteLine("File: {0}", file);
}
Basically, what I want to happen, is when the program is run without any arguments, the argument "*.*" is assumed. This has been accomplished as far as I can tell. If the program has more than one argument, it generates an error stating that the program is not being used properly, which also has been done. The problem I am having is in parsing the single parameter that is passed. I want it to be able to recognize and split the various parts of a possible string.
For example, any of the following (and many more) may be passed as a valid parameter:
- *.ext
- file*.ext
- *file.ext
- path\file.ext
- ..\file.ext
- ..\*.ext
- volume:\path\file.ext
If I run the program as-is with the paramater "..\" it works fine, because that is a valid path. but if I run the program with the paramater "*.*" or even "..\*.*", I get "System.ArgumentException: Illegal characters in path." I need to be able to separate the "..\" as the workingDirectory and the "*.*" as the fileSearchString. And not to mention, if there is *also* a volume in front of the path, I need to get that too.
I hope you get the point. What I need is to parse out the volume (if it exists - otherwise use current), the directory (if it exists) and the file search pattern (exact filename or wildcard). How should I go about writing the code for my switch case of "1"?