0

Consider the following code, which simply starts a process from VB.NET:

Public Shared Sub ShellandWait(ProcessPath As String, Optional Arguments As String = "")

    'Starts a process/program and waits for it to finish before execution continues.

    Using objProcess As New System.Diagnostics.Process

        objProcess.StartInfo.FileName = ProcessPath
        objProcess.StartInfo.Arguments = Arguments
        objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        objProcess.StartInfo.ErrorDialog = False

        objProcess.Start()

        'Wait until the process passes back an exit code 
        objProcess.WaitForExit()

        'Free resources associated with this process
        objProcess.Close()

    End Using

End Sub

If we pass in a file name for ProcessPath, say "C:\test.xml", but no program is associated with the .xml extension, Windows 10 will pop up a box asking "How do you want to open this file?"

Is there anyway to prevent Windows from opening this popup? I'd like this scenario to be handled in code instead of Windows interjecting itself.

EDIT: I don't want to find the program associated with the extension. In the majority of cases where Process.Start fails, there will be no program available to handle the file type (I just used xml as an example). My code currently warns the user that the appropriate application is not found, but the popup from Windows also shows, which I find confusing. I just want to disable the popup from showing, if possible.

Casey Wilkins
  • 2,555
  • 2
  • 23
  • 31
  • For the linked duplicate, use the answer from [Ohad Schneider](https://stackoverflow.com/a/17773402/2592875) – TnTinMn Feb 12 '18 at 19:36
  • I added an edit to try and explain why this is a different question. Please let me know if I'm still not clear. – Casey Wilkins Feb 12 '18 at 21:24
  • 2
    Your edit makes no sense, if you know there is no application to make use of the file, why would you even launch a process to attempt to open it? That is like asking I know if I hit my hand with a hammer that it hurts; I hit my hand with a hammer, why does it hurt? – TnTinMn Feb 12 '18 at 21:31
  • Because at run time I don't know if the correct application is available or not. Sure I could use a win32 API call to try to lookup the file association, or I can just let the Process.Start fail and catch it, which is what I currently do. I was hoping there was a easy way to supress the Windows popup that occurs when Process.Start fails, that's all. – Casey Wilkins Feb 12 '18 at 21:47
  • You can use `StartInfo.CreateNoWindow = True` `StartInfo.UseShellExecute = True` `StartInfo.WindowStyle = ProcessWindowStyle.Hidden`. And don't `WaitForExit()`, otherwise you will be locked there. A Try/Catch block can also be of use. In this case, Dispose() of the Process in the Finally section. – Jimi Feb 12 '18 at 22:34
  • _"Because at run time I don't know if the correct application is available or not"_ - That is why you lookup the file association. If you do so beforehand you'll know whether there is an application bound to that file type or not. If there isn't, you just don't start the process. I honestly don't see what's so wrong with doing this? It's a good solution to your problem. There is no way to do it using only the `Process` class. – Visual Vincent Feb 13 '18 at 09:55
  • 2
    @Visual Vincent Actually, with the sample code I suggested, it does work. rundll32.exe will not popup a message if the file is not associated and will run the associated opener if it is. You just need to: `Process.Start()` and `Process.Kill()` right after (so that rundll32.exe won't be stuck in memory). I do agree that this is probably not the right way to accomplish this. Preemptively checking for an existing extension association is way better. I've posted [something like this](https://stackoverflow.com/questions/47762031/start-process-a-file-without-extension-in-vb-net/47763682#47763682). – Jimi Feb 13 '18 at 16:18
  • @Visual Vincent I'm not saying that it's wrong to lookup the file association, but that doesn't actually answer my question. I wanted to know if the popup could be suppressed, to which Jimi has provided a workable-but-not-so-graceful solution. I thought that maybe there was a simple flag I was missing with Process.Start. Evidently it can't be done cleanly through Process.Start, which answers my question. Thanks to Jimi for at least proposing a solution that answers the question instead of just marking as duplicate. – Casey Wilkins Feb 13 '18 at 18:53
  • @Jimi, thanks, I'd vote your response as the answer if the question weren't closed. Appreciate your time/effort. – Casey Wilkins Feb 13 '18 at 18:54
  • I'm a bit confused regarding what you're trying to accomplish. Are you merely checking to see if the file has an application associated with it, or are you actually looking to start the associated app (if one exists)? Because if the latter then opening it as a hidden process would be a bit odd. -- I nominated your question for re-opening as I now better understand why you were looking for an answer specifically to the `Process` class. – Visual Vincent Feb 13 '18 at 19:27
  • @Jimi : I would call your sample code a workaround rather than a solution, seeing as you don't actually prevent it from opening (only you don't see it). It can also be troublesome if he's actually trying to start the associated app (however if not, then there's no problem ;) – Visual Vincent Feb 13 '18 at 19:27
  • @Visual Vincent I want to pass the file via ProcessPath, and if Process.Start() fails, have my application handle it by displaying a simple message saying that the file can't be opened. This already works within my code. I simply want to suppress the additional "What do you want to do with this file" popup that Windows generates, as the user has already been notified by the application. It can certainly be done by checking ahead of time via win32 API, but I don't think its out of the realm of reason to just be able to handle the exception without Windows interjecting the popup. That's all. – Casey Wilkins Feb 13 '18 at 21:03
  • @Visual Vincent Well, the associated app, if exists, will run. But I agree that this method is more a "curiosity" than a solution. – Jimi Feb 14 '18 at 03:28

0 Answers0