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);
}
}
Escaping of special characters (like " or ) is only needed inside of String literals within your code :
var str = "This is a literal";
var str2 = otherVariable; //This is not a literal
No need to escape the characters when calling the application.
However, when using Batch for example, there might be a different set of special characters with different types of escape characters. For example, if you want to pass a "%" (from Batch) you need to pass the escape sequence "%%".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.