DirectoryInfo throws "Argument Exception: The path is not of a legal form" for the correct path
Asked Answered
R

1

7

When I use DirectoryInfo with a specific path (say @"C:\") in my ASP.NET MVC application, it returns ok but when I try to use the exactly same path in my external C# library, it throws the above exception. I have checked the path a thousand time and the path is legal. Can somebody tell me the reason?

Edit: Here's the code:

var di = new DirectoryInfo("C:\\App\\Files\\");
    //        var file = di.GetFiles(Id + ".*").First();
    //        if (file != null) return file.FullName;
    //        return string.Empty;

The above code is used inside a property.

Thanks.

Ragi answered 18/4, 2011 at 15:30 Comment(1)
You're doing something wrong it does work - See here ideone.com/MnkVeSMarmot
S
3

From documentation:

path contains invalid characters such as ", <, >, or |.

http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.directoryinfo.aspx

See if your actual path has any of these. I know you said you have checked if the path is legal, but this is the only case. Maybe giving the exact path here will help.

Edit:

Use Path.GetInvalidPathChars() and Path.GetInvalidFileNameChars() and see if anything that is illegal has been added.

Stet answered 18/4, 2011 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.