Kirk Evans

I'm adding quick-launch buttons to a panel that allow the user to enter the name of a program, file, etc, and launch it from within the app by clicking the button. ( A lot of words for such a simple idea.)

My problem is that I can't find a good way to verify the user input.

My first naive method was to check that the file exists - but if the user enters "Notepad" (for example), my validity check will fail, because the file "Notepad" does not exist - but the string "Notepad" is valid, because it can be launched.

Short of trying to "launch the string" as a validity check, does anyone have any ideas on how to check the user input If launching the string is my only option, is there a fast, clean way to do it without having windows flashing before the user's eyes during the validity check Speed matters - loading Excel (for instance) can take a while.

Any and all thoughts on the subject are appreciated.




Re: Visual C# General How can I determine if a user-entered string is "Launchable"?

Peter Ritchie

File.Exists comes to mind. It's hard to tell what is "executable" and what is not because of file associations. I.e. anyone could add a file association for files with the extension ".asdf", and they would them be "runnable" via Process.Start, etc.






Re: Visual C# General How can I determine if a user-entered string is "Launchable"?

Kirk Evans

That's the problem I was trying (obviously not clearly enough) to state in my description of my "first naive method".

Looks to me like I'm going to have to try to launch the string entered by the user. I was hoping someone might know some quick and easy way around that. It might be ugly, but at least it will work.






Re: Visual C# General How can I determine if a user-entered string is "Launchable"?

Peter Ritchie

There's nothing specific in .NET to do what you want. You can either catch the exception and tell the user they entered an non-executable file, or you can get a registry subkey from HKEY_CLASSES_ROOT with the same name as the extension and use the default value of that subkey as the name (class name) of another subkey in HKEY_CLASSES_ROOT. That classname subkey will contain a "shell" subkey with several subkeys with respect to verbs or actions that can be performed on files with that extension. One of those subkeys is an "Open" subkey which tells Windows how to open files of that extension when their "executed". For example, if the user entered "Filename.txt", get the default value for HKEY_CLASSES_ROOT\.txt, which is "txtfile". See if there is a subkey HKEY_CLASSES_ROOT\txtfile\shell\open, and if so, get the default value for HKEY_CLASSES_ROOT\txtfile\shell\open\command (in this case "%SystemRoot%\system32\NOTEPAD.EXE %1"). You can then strip out any %x text in the text and validate that that file exists. If that file exists and filename.txt exists then you'll likely be successful with Process.Start.




Re: Visual C# General How can I determine if a user-entered string is "Launchable"?

Kirk Evans

For anyone who is interested, what I ended up with is this:

( CommonDialogs is a convenience class that basically calls MessageBox.Show() with different icons, buttons, etc )

(one other warning - i pulled these lines out of a larger method, and modified it to make sense in this posting - if you cut and paste, double check that I didn't introduce some small error in the modified version )

*** ADDED LATER : See my next post on this subject ( should be right below this one.)

public bool IsRunnable( string s ) {

try {

// using System.Diagnostics

Process test;

test = Process.Start(s);

test.Kill();

return true;

} catch {

CommonDialogs.Inform("Unable to start '" + s + "'" );

return false;

}

}

I've only just started using this, but so far it seems to fill the bill - the Kill() happens so quickly after the Start() that nothing is visible to the user.






Re: Visual C# General How can I determine if a user-entered string is "Launchable"?

Kirk Evans

Whew! One more update.

I should have tested more Sad. If i try to test input on an excel file (ex: mySpreadSheet.xls ), it takes a long time for "IsRunnable" to return. ( obviously, Excel is just and example here.. any "larger" executable would have the same problem )

anyway.. the logic works, but it isn't ready for prime-time.






Re: Visual C# General How can I determine if a user-entered string is "Launchable"?

Peter Ritchie

Kirk Evans wrote:

For anyone who is interested, what I ended up with is this:

( CommonDialogs is a convenience class that basically calls MessageBox.Show() with different icons, buttons, etc )

(one other warning - i pulled these lines out of a larger method, and modified it to make sense in this posting - if you cut and paste, double check that I didn't introduce some small error in the modified version )

*** ADDED LATER : See my next post on this subject ( should be right below this one.)

public bool IsRunnable( string s ) {

try {

// using System.Diagnostics

Process test;

test = Process.Start(s);

test.Kill();

return true;

} catch {

CommonDialogs.Inform("Unable to start '" + s + "'" );

return false;

}

}

I've only just started using this, but so far it seems to fill the bill - the Kill() happens so quickly after the Start() that nothing is visible to the user.

I don't recommend using Process.Kill. You don't know what the application is doing when Kill is called; if it had opened the file and begun writing to it and Kill is called, it could render the file corrupt.






Re: Visual C# General How can I determine if a user-entered string is "Launchable"?

Kirk Evans

Thank you, Peter.

You are correct, of course.

What should I use to terminate the process (please pardon if the answer is obvious, but I didn't see another way. )

Thanks again,

Kirk






Re: Visual C# General How can I determine if a user-entered string is "Launchable"?

Peter Ritchie

It depends on the application how it should be terminated. i.e. the user should terminate it. If you're merely testing whether a bit of text is "executable" and the user wants to execute it, just execute it. If it's not executable inform the user there was an error.

Even if you wanted to validate whether text was "executable" when it was entered, you still need to validate that it's still "executable" when you try to execute it. e.g. if the user uninstalled Word after they entered "document1.doc", it's not going to go well passing it to Process.Start...

I think parsing the registry is the only reliable means of validating whether text can be executed or not. It will never be 100% accurate because they're nothing specified exactly what Windows does, but it's better than nothing.