I'm developing a command-line utility and passing paths as arguments inside a batch file. Do I need to add the @ symbol to the paths within my application to prevent characters like "\" from escaping?
This link specifies using the @ symbol, however currently, I'm not using the @ or \\ to prevent escaping. When I pass in my path as it is, it works fine. Why is this?
I would call it like this, inside my batch file:
foldercleaner.exe -tPath: "C:\Users\Someuser\DirectoryToClean"
Program:
class Program
{
public static void Main(string[] args)
{
if(args[0] == "-tpath:" || args[0] == "-tPath:" && !isBlank(args[1])) {
clearPath(args[1]);
}
else{
Console.WriteLine("Parameter is either -tpath:/-tPath: and you must provide a valid path");
}
}
Clear Path Method:
public static void clearPath(string path)
{
if(Directory.Exists(path)){
int directoryCount = Directory.GetDirectories(path).Length;
if(directoryCount > 0){
DirectoryInfo di = new DirectoryInfo(path);
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
}
else{
Console.WriteLine("No Subdirectories to Remove");
}
int fileCount = Directory.GetFiles(path).Length;
if(fileCount > 0){
System.IO.DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
}
else{
Console.WriteLine("No Files to Remove");
}
}
else{
Console.WriteLine("Path Doesn't Exist {0}", path);
}
}