Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine via C# whether a string is a valid file path

I would like to know how to determine whether string is valid file path.

The file path may or may not exist.

like image 276
Diskdrive Avatar asked Jun 18 '10 05:06

Diskdrive


People also ask

How do you calculate average in C?

scanf("%f", &num[i]); And, the sum of each entered element is computed. sum += num[i]; Once the for loop is completed, the average is calculated and printed on the screen.

What is quadratic equation in C?

The standard form of a quadratic equation is: ax2 + bx + c = 0, where a, b and c are real numbers and a != 0. The term b2; - 4ac is known as the discriminant of a quadratic equation. It tells the nature of the roots.

How do you find the quotient in C?

Similarly, the remainder is evaluated using % (the modulo operator) and stored in remainder . remainder = dividend % divisor; Finally, the quotient and remainder are displayed using printf( ) . printf("Quotient = %d\n", quotient); printf("Remainder = %d", remainder);


1 Answers

You can use the FileInfo constructor. It will throw a ArgumentException if "The file name is empty, contains only white spaces, or contains invalid characters." It can also throw SecurityException or UnauthorizedAccessException, which I think you can ignore if you're only concerned about format.

Another option is to check against Path.GetInvalidPathChars directly. E.g.:

boolean possiblePath = pathString.IndexOfAny(Path.GetInvalidPathChars()) == -1; 
like image 91
Matthew Flaschen Avatar answered Oct 14 '22 10:10

Matthew Flaschen