locked
Process.Start() throws WIN32 exception but not with every file RRS feed

  • Question

  • I am using the following code to open every file, using my application.

    LobjProcess = new Process();

    LobjProcess.StartInfo.FileName = "SomeFile.xyz";

    LobjProcess.Start();

    The problem is - If there is no program attached to certain file types in windows registry, with some of the file extensions I get Win32 exception while for few I get the default Windows Dialog box - "Windows can't open this file. Use Web or select a program to open the file".

    While if I try to open the same files from the file System, with all these files I get the default Windows Dialog box. Why this behavior is not consistent when I am doing it from code? Can anyone help me with the solution? 

    Thanks.
    Mukta 


    M Sharma, TCS, India
    • Moved by Paul Zhou Wednesday, March 16, 2011 6:43 AM not supported (From:.NET Base Class Library)
    Tuesday, March 8, 2011 7:49 PM

Answers

  • Hmm.. interesting. One last option, let the exception happen and show the dialog manually. The below code should solve your problem. If not, there is some problem with your OS.

    try
    {
       ...
       LobProcess = new Process();
       LobProcess.StartInfo.FileName = "Somefile.xyz";
       LobjProcess.Start();
    }
    catch(Exception)
    {
       string command = "RUNDLL32.exe";
       string arguments = string.Format("SHELL32.DLL,OpenAs_DLL {0}", "Somefile.xyz");
       Process.Start(command, arguments);
    }
    

     Hope this time no issues you find ;)

     


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    • Proposed as answer by Kris Anderson Tuesday, March 15, 2011 12:05 PM
    • Marked as answer by M Sharma Wednesday, March 16, 2011 2:37 PM
    Monday, March 14, 2011 3:02 PM

All replies

  • Your problem is simple. consider following two scenarios.

    1. you have a notepad.exe file. When you double click on it, the exe launches itself.

    2. You have a MyFile.txt. When you double click on it MyFile.txt doesn't automatically launch itself. But, OS searches for the application to which the .txt files are asociated. So, Notepad.exe is used to open .txt files. Hence, it runs Notepad.exe to open contents of MyFile.txt.

    3. With above two points in mind, When you double click on a file say "SomeFile.xyz", OS doesn't understand what kind of extension it is. So, it displays dialog saying "Windows can't open this file. Use Web or select a program to open the file".

    The same thing happens for Process.Start() as well. You can't simply pass any file to this. For example, if you want to open MyFile.txt, you must pass the associated application i.e. NotePad.exe & MyFile.txt as parameter Like below.

    Process.Start("NotePad.exe", "MyFile.txt");
    

    Similarly if you want play MySong.mp3 file, then you may do some thing like,

    Process.Start("wmplayer.exe", "MySong.mp3");
    

    But you can't simple do

    Process.Start("MyFile.txt");  OR  Process.Start("MySong.mp3");
    

    Hope this helps you.


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    Wednesday, March 9, 2011 3:26 AM
  • Hello Adavesh, 

    Thanks for helping on this. I understood what you said.

    But I wud like to mention here that untill Windows shows me  "Windows can't open this file. Use Web or select a program to open the file" message, I am good. 

    But My problem is Process.Start() is throwing exception for those files which has some registry entry for some reason but they are not associated with the real executable. 

    For e.g. lets say, I have a file type .ssk. Whenever this file is used on some user's machine, I want him to see a specific icon for these files. To accomplish this I have made a regisrty entry. When I try to open this .ssk file from file system, windows show me a message "Windows can't open this file. Use Web or select a program to open the file". 

    But if I try to open the same file using Process.Start(), as there is no real exe available for these kind of files, I get WIN32 exception that application not found. I want "Windows can't open this file. Use Web or select a program to open the file" instead of exception here too. 

    Hope It make sense !! 

    Regards.


    M Sharma, TCS, India
    Thursday, March 10, 2011 12:26 AM
  • Hi Sharma,

    The dialog should automatically launch. If it doesn't lanch and throwing exception, you have to run RunDLL32.exe which is there in C:\Windows\System32. Just run it by double clicking (note that double clicking rundll32.exe doesn't launch any window). Once you run that exe, try running your program. Now it will show the Windows can't open this file. Use Web or select a program to open the file"  dialog.

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    • Proposed as answer by Paul Zhou Friday, March 11, 2011 9:09 AM
    • Unproposed as answer by M Sharma Monday, March 14, 2011 2:07 PM
    Thursday, March 10, 2011 6:19 AM
  • I tried the same running RunDLL32.exe still the issue is reproducible. I want to mark here that I am using Windows 7. 

    Only one type of file which already has a registry entry, still throws an error.

     

    Thanks 

    Mukta 


    M Sharma, TCS, India
    Monday, March 14, 2011 2:09 PM
  • Hmm.. interesting. One last option, let the exception happen and show the dialog manually. The below code should solve your problem. If not, there is some problem with your OS.

    try
    {
       ...
       LobProcess = new Process();
       LobProcess.StartInfo.FileName = "Somefile.xyz";
       LobjProcess.Start();
    }
    catch(Exception)
    {
       string command = "RUNDLL32.exe";
       string arguments = string.Format("SHELL32.DLL,OpenAs_DLL {0}", "Somefile.xyz");
       Process.Start(command, arguments);
    }
    

     Hope this time no issues you find ;)

     


    Please mark this post as answer if it solved your problem. Happy Programming !!!
    • Proposed as answer by Kris Anderson Tuesday, March 15, 2011 12:05 PM
    • Marked as answer by M Sharma Wednesday, March 16, 2011 2:37 PM
    Monday, March 14, 2011 3:02 PM
  • Hi M Sharma,

     

    Welcome to the MSDN forum!

     

    Does Adavesh’s solution works on?

    Actually, it is not a problem supported in this forum.

    I suggest that you can post thread in Windows General Development Forum.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 15, 2011 2:51 AM
  • Thanks Adavesh !!! This woks very fine. The only update is, the command should be OpenAs_RunDLL rather than OpenAs_DLL

    Regards
    Mukta 


    M Sharma, TCS, India
    Wednesday, March 16, 2011 2:39 PM