Using OS to open files

I am currently writing an app that extracts files from a database and attempts to open them using Process.Start();

It works fine as long as the file type has an association, but if there is no association, I get an exception.  What we are looking to have is for the OS to handle it just as if you tried to open a file from Windows Explorer and the file type is unassociated, you get a Windows Dialog (Windows cannot open this file)

What do you want to do

Use the Web service to... or Select the program from a list.

The current code:

try

{

// open the file in a new process

Process otherProcess = new Process();

otherProcess.StartInfo.FileName = strPath + "\\" + strAttachName;

otherProcess.StartInfo.Verb = "Open";

otherProcess.StartInfo.CreateNoWindow = true;

otherProcess.StartInfo.UseShellExecute = true;

otherProcess.Start();

//otherProcess = Process.Start(strPath + "\\" + strAttachName);

otherProcess.WaitForExit();

}

catch (Win32Exception e)

{

if (e.NativeErrorCode == 1155)

{

Process otherProcess = new Process();

otherProcess.StartInfo.FileName = strPath + "\\" + strAttachName;

otherProcess.StartInfo.Verb = "OpenAs";

otherProcess.StartInfo.CreateNoWindow = false;

otherProcess.StartInfo.UseShellExecute = true;

otherProcess.Start();

otherProcess.WaitForExit();

}

}

Does anyone know how I can achieve this

Thank you



Answer this question

Using OS to open files

  • Mike Schetterer -- MS

    Sorry, pass everything after the rundll32.exe as the Arguments property of the Process object.


  • Ash Wahi

    When you get the execption trying to open it, run this as the process

    "rundll32.exe shell32.dll,OpenAs_RunDLL " + FileNameVariable

    This will pop up the shell extension for the particular file.


  • Zahid Aziz

    Ahh, Yes thank you...works perfectly now.
  • NightwoIf

    Hi Justin,

    Thanks for your reply.  I changed my code to:

    string strFileName = strPath + "\\" + strAttachName;

    Process otherProcess = new Process();

    otherProcess.StartInfo.FileName = "rundll32.exe shell32.dll,OpenAs_RunDLL " + strFileName;

    otherProcess.Start();

    otherProcess.WaitForExit();

    and now I'm getting The System cannot find the file specified, but I know the file to open is there.  If I try the same string from Run, I get the windows dialog that I was expecting.  Do you have any ideas

     

    Thanks again


  • Using OS to open files