Tuesday, August 23, 2016

Associating any file with an installed application

Recently I got a requirement to associate a file having custom extension with the installed version of our own application. So, whenever user clicks on that given extension file, it should open up directly in my WPF application along with the data population. So, let’s check out how you can achieve this:

Step 1 - Creating an application: Create a WPF application or you can take any of your existing application. I’m working on WPF application (using MVVM) but the concept remains same for others, you can go with your Windows Forms or Console application also. That’s pretty acceptable. In my application, I'm taking few customer properties and a file path.

Step 2 - Get key for Signing: For signing my assembly I’m using inbuilt feature of Visual Studio. Go to your project properties, navigate to Signing option as shown below:

                                            








If you already have a key then you can browse and associate it else you can always go ahead and create a new key. Please note: You can use any other way to sign your assembly. In order to make this post simple, I’m using the easiest way J
Once the key is created, it will be automatically added to your project with name as <xxx>.pfx

Step 3 - Signing the manifest: Now as your key is ready, next step is to associate it with your deployment. This can be done by updating the ClickOnce manifests section under Signing tab as shown below:











On the above screen check the given checkbox and browse your pfx file. Note: Please verify expiry date of the certificate once it is browsed successfully.

Step 4 - Associating a File: Next step is to associate our custom file extension, for me it is .shw (this .shw file is in JSON format). Similarly you can take any extension that you would like to associate with your WPF application.  Let’s quickly go to Publish option and furnish all the required details as shown in below image:

















Add default icon for your application using Application Files dialog. Once the icon is associated with your application, you will be able to see that in Application Files dialog. By any chance, if you can’t find your icon in list, please follow my previous post for troubleshooting this J

Step 5 - Tweak your application: Now most important step is to update our WPF application, so that it can understand this new extension (.shw).

Update application startup with following code: 
protected override void OnStartup(StartupEventArgs e)
   {      
      if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null
               && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0)
            {                

               string fileName = "";
               try
                {  
                   fileName = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];
                   Uri uri = new Uri(fileName);
                   fileName = uri.LocalPath;

                   AppShared.SelectedFilepAth = fileName;

                }                

               catch (Exception ex)
                {                   

	  // do exception handling
                }
      }              
       base.OnStartup(e);
    }

Update your ViewModel so that your view can read data as:

public MyViewModelConstructor()
        {                     

             if (!string.IsNullOrEmpty(AppShared.SelectedFilepAth))
             {

                Customer = Newtonsoft.Json.JsonConvert.DeserializeObject<Customer>(System.IO.File.ReadAllText(AppShared.SelectedFilepAth));
                FilePath = AppShared.SelectedFilepAth;
             }            

            else
            {                

                Customer = new Customer() { Name = "Test User", Age = 100 };
            }
        }
 
Step 6 - Final Step: Build your code. Publish it. Install it and wow you are done. After performing all these steps, try double clicking your file, it will automatically pop-up your application with pre-loaded data.
 
It's straight forward. Isn't it?

Ok, let's do something more. Till now we were doing our file association via nice and user friendly GUI. But there is an alternate way available to achieve the same for our command-line lovers in which they have to play with application manifest file directly. Below are the steps to perform the same operation using command line:
  • Open you application manifest. In case, if it is not available please add a new app manifest file to your project and open it into the editor of your choice.
  • Add a new fileAssociation element as a child of Assembly element.
  • Add 4 attributes to this newly added fileAssocitation element – extension, description, progid and defaultIcon. Sample manifest file is given here, In case if you want to associate more file types, then you have to add another fileAssocitation element.
  • Sign application manifest using Mage.exe utility:
mage -Sign <YourApp>.exe.manifest -CertFile <yourCertificate>.pfx

Entire list of Mage command parameters is given over here.
Hope you enjoyed associating your file with multiple ways. Create a file with funny extension, open it and have fun. Happy learning!

Tuesday, August 16, 2016

Error: Icon file is not set to be published...

Today while working on one of the application that was for ClickOnce deployment I faced a small issue which took almost my half an hour. My task was to associate a default icon to my application. So, in order to do this, I went to my project properties and simply set the icon as shown below:
















As you can see in above screenshot, it is showing an error icon which states: 'Icon file is not set to be published with the application, or is not part of the required download group'

Then I build my application and land up with below error message which was more clear:






After hitting my head for many minutes, I thought, let's check out what is this 'download group'.

On surfing net, I got the clue that download group is nothing but a collection of files which are going to be part of our publish activity.
So, I quickly opened Application Files dialog using Project properties >> Publish. The Application Files dialog looks like:















I noticed that my icon file is not listed above. Then I got an idea on what went wrong :)
I immediately changed the to build action to 'Content' and force it to 'Copy Always'. And guess what?
My error is gone :)

After fixing this issue, I realized that it was very silly. As it wasted my many minutes, I thought to add it to my repository, so that it can be useful for all others who got stuck like me :(

Happy troubleshooting !!! 

Sunday, August 14, 2016

Back to blogging

Hey friends, I’m back to my blogging world after a gap of few months. Yes, you are right. I’m alive. Actually I was busy with motherhood J

Last night, I was scared to look at my blog, fearing all the followers have given up on me. Trust me, this is very-very scary feeling. Although it’s been not so long since I become a blogger but a fear of losing followers, who actually take time to read my posts is something which I can’t express. Hope you are reading this.

After a long break, it is bit difficult to get the same momentum. But I’ll try to make it up. So, stay tuned and keep reading.

Happy learning!!!