Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ShellExecute

jkjk Posts: 7Member
Hi - in .NET Framework, I need to do something similar to the ShellExecute in the Win32 API. What I need to do is open a file with whatever program its file type is associated with in Windows.

I found the Shell command in .NET but it appears that you have to provide it with the EXE, which is not what I need to do.

I assume there has to be something more elegant in .NET Framework than using the Win32 API ShellExecute.

Any help would be much appreciated.
Thanks.

Comments

  • DataDinkDataDink Posts: 135Member
    All file associations are stored in the registry - I believe shellexecute is simply looking up these associations and sending the appropriate application a command-line.

    If you don't want to do this I have also just sent the file to "explorer.exe" because explorer.exe will for the most part launch the appropriate association.
  • jkjk Posts: 7Member
    Good suggestion about using Explorer, thanks!
  • jkjk Posts: 7Member
    It turns out that using Explorer isn't working so well for users.

    The filetype I'm opening in code is .jpg and on a user's machine it's associated with the Windows Picture and Fax Viewer.

    However the following line of C# code always opens the .jpgs with Internet Explorer, and not the app that .jpg is associated with:

    Interaction.Shell(theExe + " " + imagePath + filName, AppWinStyle.MaximizedFocus, false, -1);

    The variable theExe is a string containing the path to the Windows Explorer executable on that computer. I've got a method that produces that, the core of which is:

    string searchStr;
    foreach (DriveInfo drvInf in DriveInfo.GetDrives())
    {
    //Have to use Fixed drive type to avoid situation in Citrix where
    //we were getting the drives from the computer that logged in
    //to Citrix (in addition to the Citrix machine's drives).
    if (drvInf.DriveType == DriveType.Fixed)
    {
    searchStr = drvInf.Name;
    searchStr = searchStr + @"Windowsexplorer.exe";
    FileInfo filInf = new FileInfo(searchStr);
    if (filInf.Exists)
    {
    return searchStr;
    }
    }
    }

    --Any ideas on how I can get it to really use the associated file type?
    Thanks.
  • jkjk Posts: 7Member
    I figured out a much better way to do this without using ShellExecute.

    There's a class called Process under the System.Diagnostics namespace that's very useful. Kind of a strange place to put it since you'd think System.Diagnostics would be all about error handling.

    Anyway, here's an example of how you can use StartInfo and then Start to open a file with whatever application it's type is associated with:

    System.Diagnostics.Process myTestProcess = new System.Diagnostics.Process();

    myTestProcess.StartInfo.FileName = @"c:filesjpg1.jpg";
    myTestProcess.Start();

Sign In or Register to comment.