Change file association for .PDF programmatically to associate it with my app

Darren Rose 261 Reputation points
2022-07-07T21:38:16.877+00:00

Hi

I have a PDF viewer I have written and I want to have a button on the settings page to set it as the default viewer for .PDF files, but I am struggling to work out how to do this.

e.g. the VB.NET code equivalent of right clicking on a PDF file and choosing open with and setting it as the default app / always open with.

Any pointers please?

If I can't do it automatically in code - or if needing admin rights is going to be a deal breaker (app will be run by normal users on a domain so they won't have admin rights!) then perhaps I can do something similar to how Adobe Acrobat Reader manages to guide you through it by automatically opening the properties page of a PDF file and telling you to click the Change button to set the default app - as per video clip here - https://www.screencast.com/t/50YTxhexSG

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,778 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Darren Rose 261 Reputation points
    2022-07-08T12:56:16.373+00:00

    Thank you both for your replies, I have now got it working in a suitable way.

    I have a button in my app which opens the properties page of a PDF file using the following code (found here - https://www.daniweb.com/programming/software-development/threads/389833/open-properties-window-of-files-programmatically)

    Private Sub btnChoose_Click(sender As Object, e As EventArgs) Handles btnChoose.Click  
      
            Dim sp As New SHOW_PROPS(System.IO.Path.Combine(My.Application.Info.DirectoryPath, "Resources\USER GUIDE.pdf"), CType(Me.Handle, IntPtr))  
      
    End Sub  
      
      
    Public Class SHOW_PROPS  
      
        Public Const SW_SHOW As Short = 5  
        Public Const SEE_MASK_INVOKEIDLIST As Short = 12  
      
      
        Private m_fileName As String  
        Private m_handle As IntPtr  
      
      
        Public Sub New(ByVal fileName As String, ByRef hndl As IntPtr)  
            m_fileName = fileName  
            m_handle = hndl  
            Call ShowProperties()  
      
        End Sub  
      
        Public Structure SHELLEXECUTEINFO  
            Public cbSize As Integer  
            Public fMask As Integer  
            Public hwnd As IntPtr  
            <MarshalAs(UnmanagedType.LPTStr)> Public lpVerb As String  
            <MarshalAs(UnmanagedType.LPTStr)> Public lpFile As String  
            <MarshalAs(UnmanagedType.LPTStr)> Public lpParameters As String  
            <MarshalAs(UnmanagedType.LPTStr)> Public lpDirectory As String  
            Dim nShow As Integer  
            Dim hInstApp As IntPtr  
            Dim lpIDList As IntPtr  
            <MarshalAs(UnmanagedType.LPTStr)> Public lpClass As String  
            Public hkeyClass As IntPtr  
            Public dwHotKey As Integer  
            Public hIcon As IntPtr  
            Public hProcess As IntPtr  
        End Structure  
      
      
        <DllImport("Shell32", CharSet:=CharSet.Auto, SetLastError:=True)>  
        Public Shared Function ShellExecuteEx(ByRef lpExecInfo As SHELLEXECUTEINFO) As Boolean  
        End Function  
      
        Private Sub ShowProperties()  
      
            Dim sei As New SHELLEXECUTEINFO  
            sei.cbSize = Marshal.SizeOf(sei)  
            sei.lpVerb = "properties"  
            sei.lpFile = m_fileName  
            sei.nShow = SW_SHOW  
            sei.fMask = SEE_MASK_INVOKEIDLIST  
            sei.hwnd = m_handle  
      
            Try  
                ShellExecuteEx(sei)  
            Catch ex As Exception  
                MsgBox(ex.ToString & vbCrLf & ex.StackTrace.ToString)  
            End Try  
        End Sub  
      
    End Class  
    

    I have added my app to the registry at the following location so it appears in the "choose your app" dialog

     [HKEY_CLASSES_ROOT\Applications\myapp.exe\shell\open\command]  
     @="\"C:\\myapp.exe\" \"%1\""  
    

    So it works as below which is perfect, as it doesn't need admin rights and gives the user the choice as to whether to see my tool or another tool as the default for PDF files

    218907-tmp1.jpg

    1 person found this answer helpful.
    0 comments No comments

  2. Jiachen Li-MSFT 33,446 Reputation points Microsoft Vendor
    2022-07-08T02:01:19.203+00:00

    Hi @Darren Rose ,
    The following code may help you.

            Dim fKey = Registry.ClassesRoot.OpenSubKey(".pdf")  
            Dim fType = fKey.GetValue("")  
            Dim myExecutable As String = Assembly.GetEntryAssembly().Location  
            Dim Command As String = "\" + myExecutable + " \ " + "%1\"  
            Dim keyName As String = fType + "\shell\Open\command"  
            Using key = Registry.ClassesRoot.CreateSubKey(keyName)  
                key.SetValue("", Command)  
            End Using  
    

    Links you can refer to:
    https://stackoverflow.com/questions/7789319/how-to-make-a-c-sharp-application-work-as-the-default-program-for-certain-files
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.