Pregunta

This is very common thing but i am very confused to get around this.

Taking file path as command line argument in c#.

If i give input "F:\\" then this works perfect.

But when i give input "F:\" it gives output like F:".

I know this is because of backslash escape character.

But my problem is how to get around this without modifying user input because logically user input is correct.

Is it possible without modifying user input get correct path in this situation?

I also know that there is @ character which can be used.

But as i said this is command line argument so the string is already in variable.

I also read some blogs but still i remain unable to resolve my problem.

C# Command-Line Parsing of Quoted Paths and Avoiding Escape Characters

EDIT :Actually my program is to list all the files inside directory so i am first checking for Directory.Exists(command line arguments) and then getting list of all the files if directory exist.

Ok so in that case when user gives Command line argument as i shown above logically the drive exist but just because of escape character it returns false.

Just think about printing the command line argument as follow.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("{0}", args[0]);

        Console.Read();
    }
}

I am having very less knowledge of c# thanks for helping.

¿Fue útil?

Solución

Im not sure why your having a problem here. In M$ Windows, a directory can be specified with or without a back-slash so all of these are correct: c: and c:\ and c:\media and c:\media\. This is the same for Directory.Exists(path) and other functions like Directory.GetFiles(path).

Ths following is a very simple app to list directory files and in my environment it works regardless of whether I put a slash on the end. So c:\media\ gives me all my media files.

class Program
{
    static void Main(string[] args)
    {
        string path = args[0];
        Console.WriteLine("trying path: " + path);
        if (Directory.Exists(path))
            Directory.GetFiles(path).ToList().ForEach(s => Console.WriteLine(s));
        else
            Console.WriteLine("path not found");
    }
}

One thing to note is that in visual studio, when using the debugger such as Quick Watch, it will show the escape character with backslashs. So if user enters c:\media\ the string will be stored as c:\media\ but when you quick watch the path in VS you'll see c:\\media\\; look deeper with the Text Visualisation feature and you'll see the path correctly shown as c:\media\.

Otros consejos

You should use Path class and specifically Path.GetFullPath method to get correct full path.

class Program
{
    static void Main(string[] args)
    {
        string path = Path.GetFullPath(args[0]);
        Console.WriteLine("trying path: " + path);
        if (Directory.Exists(path)){
            var files = Directory.GetFiles(path);
            foreach (var file in files) Console.WriteLine(file);
        }
        else
            Console.WriteLine("path doesn't exist");
    }
}

UPD. Paths with spaces should be passed in quotes. Or you should concat all your command line arguments, if path is the only input.

Use Environment.GetCommandLineArgs(). It will clean up the path.

See this link for more info: http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs(v=vs.110).aspx

Well, if I understand correctly. You can use string.Format . There are overload methods that could help you without modifying much user input.

Sample code:

string[] inputs = ...
string output = string.Format("F:\\{0}\\{1}", inputs[0], inputs[1]);

C# interprets \ as an escape character. So \" is interpreted as " Possible way to fix it (if you are sure that there is no " inside arguments:

string a = args[0].Replace('"', '\\');

I think your over thinking this. Logically that isnt valid input. \ is an escape character on the command prompt just as it is in c# inside of a string. What you have entered ("F:\") is an invalid value and it IS on the user to correct. The user is saying at this point that they want the quote.

Note that when you pass the filename in parameters, you need to have the access rights in the directory where file is placed, otherwise some parts of your application might fail unexpectedly and it might took you much time to figure out what's wrong there.

    var args1 = Environment.GetCommandLineArgs().Skip(1);
    if (args1 != null && args1.Count() > 0)
    {
        //do stuff
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top