user1202434
user1202434

Reputation: 2273

How to Check if a File Path is Valid

I have a string that is in the registry that can either point to an .exe, .txt along with certain command line arguments like:

C:\\PAthtoTheExe\program1.exe -arg1 -arg2

How do I determine if the "program1.exe" is a valid executable, IE that it points to an existing file? (this could be removed if the user uninstalls the application for eg:)

I don't like the idea of parsing out the arguments, because right now, the path can either point to an .exe or .txt or a .url. I would have to put in a long if else to do that.

Upvotes: 0

Views: 361

Answers (1)

Bob Kaufman
Bob Kaufman

Reputation: 12815

There's a

File.Exists( path );

method that will return true if the filename exists. It won't tell you if the file contains a valid executable, though.

You can separate the path from the other arguments using

string tokens[] = registry_value.Split( ' ' );

then passing the first token to File.Exists()

Upvotes: 1

Related Questions