CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2010
    Posts
    56

    Open File without an associated filetype.

    I have a particular file that I am trying to open from my .NET application. The file may or may not be associated with a program by default. For the most part users will open the file in notepad.

    The problem is that when I attempt to open this file with Start.Process() if the user has not associated a program with this file type it will crash.

    Is there a way to pre-associate notepad with this file by default if the user does not have an associated application assigned to the file type?

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Open File without an associated filetype.

    Well, you can use something like this to open a file in Notepad:
    Process.Start("Notepad.exe", "C:\\myPath\\myFile.xyz");

    When you say that it crashes, do you mean you get an exception? If so, what does it say?

  3. #3
    Join Date
    Jun 2010
    Posts
    56

    Re: Open File without an associated filetype.

    Running it by itself it would say the program has crashed and would request I send a report to Microsoft, it wasn't a thrown exception or a "this program has stopped responding". When I attached it to a debugger it would simply close out the program without throwing an exception.

    At this point I realized the file we were opening was not associated with a program so I associated it with notepad and it wouldn't crash anymore.

  4. #4
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Open File without an associated filetype.

    Don't know for sure, but I'm guessing you would need to check in the registry for extension association before launching the file... if it isn't associated, launch via "notepad.exe filename" as suggested by 'thegreat'.

    Code:
    bool isAssociated = (Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(".ext", false) != null);
    (found that code at http://mel-green.com/2009/04/c-set-f...e-association/ )
    Last edited by fcronin; June 8th, 2011 at 05:14 PM.

  5. #5
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Open File without an associated filetype.

    We have a small utility function that tells whether the System knows how to open the file.
    Code:
    private bool FileIsShellOpenable(string FileName)
    {
        if ((String.IsNullOrEmpty(FileName) == false) && File.Exists(FileName))
            return
                ((File.GetAttributes(FileName) & FileAttributes.NotContentIndexed)
                    != FileAttributes.NotContentIndexed);
        }
        else
        {
            return false;
        }
    }

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Open File without an associated filetype.

    What's unusual is that the system should present you with the "Open with..." window.
    The only thing I can think of that would cause the app to crash, is to specify the name of the file to open without the extension (but, OTOH that would still throw an exception).

  7. #7
    Join Date
    Jun 2010
    Posts
    56

    Re: Open File without an associated filetype.

    Removed the file association on my system and it throws back an exception that my debugger caught.

    Now on the other system I don't have any form of debugger so it might have hit the exception and just crashed on the exception.

  8. #8
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Open File without an associated filetype.

    You don't need the debugger to catch an exception, your application can do that.
    Code:
    try
    {
        // put sensitive code here... (e.g. try to open)
    }
    catch(Exception exc)
    {
        // put exception-handling code here... (e.g. open with Notepad)
    }
    To find out more see:
    Exceptions and Exception Handling (C# Programming Guide)
    try-catch (C# Reference)


    But you haven't answered my (although implicit) question: does the string you pass contains the extension of the file as well or not?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured