Opening files from Windows Explorer in my Metro App - c#

Coming from a Windows Forms background, I am used to being able to handle arguments, in the Program.cs file, passed to my application, when a user tries to open a Text file from Windows Explorer, so that my application can display its contents to the user.
However, in Metro style Apps, we don't have the Program.cs file anymore. We have the App.xaml or App.xaml.cs file.
Seeing as though I cannot find relevant documentation on this, I could just try doing it "the usual" way, in the App.xaml.cs file but I'm not even sure if that's the right way to go about it. I have Added the appropriate Capabilities and File type Associations to my Metro style App, but other than that I am don't know where to start.
How can we open a supported file from the Documents folder into our own Metro style Apps?

See How to handle file activation # http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh779669.aspx

You handle this by two specific steps:
Declare the file extension in your manifest. You can do this by opening the package.appxmanifest from Solution Explorer in VS, Going to the Declarations Tab, and adding the "File Type Association" declaration & relevant information.
In your activation handler, you will see the even has a "Kind" parameter. This will be "file" for a file launch (from explorer, or elsewhere). You will get the files in the "files" property on the same object.
Full details are here. Once you've got the files, you can use the standard Windows.Storage API's to access those files.

open package.appxmanifest in Solution Explorer.
Select the Declarations tab.
Select File Type Associations from the drop-down list and click Add.
Enter txt as the Name.
Enter .txt as the File Type.
Enter “images\Icon.png” as the Logo.
add the proper icons in the app package
and in c#, You need to handle OnFileActivated event
protected override void OnFileActivated(FileActivatedEventArgs args)
{
// TODO: Handle file activation
// The number of files received is args.Files.Size
// The first file is args.Files[0].Name
}

Related

Include and launch PDF from C# Visual Studio Project

In my C# application i have a help button. When it's pressed I would like for the application to open up a PDF file in the systems default PDF reader, something I can do with a command like Process.Start("pathToPDF").
The problem is that I would like to include the PDF as a resource instead of calling an external file. I do not wish to copy the PDF to the users computer and do not want to host it online or on a NAS.
Right click on your project in the Solution Explorer, then add existing file and choose your pdf (if you cannot find it, make sure you are showing all files and not just .cs files etc.).
Click on the newly added item once in the solution explorer and in the properties window, you set Copy to Output Directory to Copy Always or Copy if newer.
Now you can open the pdf file as expected using Process.Start(filename.pdf);
The only Secure way to show a PDF without providing a file is to include your own Viewer Component (Ex. http://www.o2sol.com/pdfview4net/overview.htm)
Some components allow to load a PDF from Memory (as in a embedded Resource) directly into your Viewer Component, another way would be to create an encrypted binary file to ship with your application and encrypt/load when necessary.
As soon you want to show the PDF in an external viewer ,be aware that the User will have the ability to save the PDF anyway.
Maybe you can explain your reasons to not want to include the file, so we can suggest other solutions to you?
Update:
As noted in your comment, the goal is to have a clean installation.
It would be possible to embed the File as a resource, but then you would
have the problem that if you extract the file temporarily to display it, you can't really control the clean-up of that file, because it's locked by the PDF Reader Application.
So you would end up with the PDF File anyway ;)
What you can do to keep your Application Folder cleaner, is to not install the PDF under that Application Folder but under the "Common Documents" Directory.
For Example: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), "MySoftware", "Help.pdf")
Which normally targets: C:\Users\Public\Documents\MySoftware\Help.pdf

Launch from file when app already open in Universal app

I am currently developing a specialized file editor application for the UWP platform. Right now, I have a file association so that a user can click on a file to load it into the app. This loads the file into the "working files" list which allows the user to edit multiple files in a single instance like Visual Studio Code.
Right now, if a user clicks on a file to open it will launch the app and load the file via the OnFileLaunched event. But what I also want to do is load a file when a user clicks on a file and the app is already open to load it into the program. What do I need to do for this?
Take a look on OnActivated method.
You can use OnFileActivated method to handle event when application is open and file clicked

File extension file association and ClickOnce

I want to create my own extension without changing my registry file. I have a VB.NET project, and I first want to create my own extension like ".abc". Afterwards, if there is any file like xxxxxx.abc when a person tries to open this file, I want Windows to start up my project and open that file.
The comment above saying that it is impossible is a little misleading.
This is impossible to do without changing the registry, but for a clickonce application it is totally possible to do without having to write any code yourself.
When you create a file association in a clickonce application the required information is written to the HKEY_CURRENT_USER hive, and overrides whatever is set at the HKEY_LOCAL_MACHINE level. When your application is uninstalled the file association entries are also removed from HKCU, which means any association already set in HKLM (if any) will be used from then on. IOW you can only override (or set) the file association on a per-user basis.
For more information on how to set the file association, check these links:
MSDN: How to: Create File Associations For a ClickOnce Application
Stack Overflow: How do you set up a file association with a click-once application?
Stack Overflow: ClickOnce File Association
Note that there is also a dialog built into VS2010 to help set this up:
right click on the project in the Solution Explorer, select Properties
select the Publish tab
select the Options button
select the File Associations option

How do I associate a filetype with an icon?

I have written preview handlers for a couple custom file types. These file types don't have an application by default that can open them. I want to associate an icon with the file types as a signifier that they are custom.
I have tried setting a registry entry for DefaultIcon in HKCR\.<filetype>, but it doesnt appear to work, even after reboots. I do these registry modifications from C#, and the modifications are successful.
The icon file is simply an ico file residing in %APPDATA% for the moment. I plan to add a few more icons, and put these in a dll file. I just want to get it to work before I take those next steps.
How can I set this icon?
Here is a simple .REG file that registers a icon for the file extension .tj
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.tj]
[HKEY_CLASSES_ROOT\.tj\DefaultIcon]
#="D:\\scratch\\testy\\icons\\tj.ico"
Tested on Win7
This MSDN article explains how to assign custom icons to file types

a Problem with Windows Help Button (EDIT-New problem)

a strange behavior happens when install the application and then run the exe file.
first let me explain my steps:
1) i added HelpProvidor and i set HelpNameSpace property to the chm file path.
2) in the form properties i set HelpNavigator on helpProvidor property to : KeywordIndex
and HelpString on HelpProvidor to Use ASGATech Quran Sound Downloder Help
3) i created an installation pkg. and it runs successfuly.
My problem is when i debug the application and presses on "?" (which is the help button) it displays the chm file.
but when i run the sortcut on the desktop which has been created from the installation package it doesn't.
why is that the HelpNamespce is the same. and the exe which is in the Application folder is that same so why is that.
the exe file is the same and also when i created a new item in start -> program menu the exe file that created runs the chm file but the shortcut doesn't.
i put the chm with the application folder which throws the application folder on:
c:\program Files\Manufacturer\ProductName
and it put the file on this path successfuly so why he can't see it.
EDIT:
i found that the problem is when i uninstall the application it keeps the product folder
it only removes the icon ans chm and so when u reinstall the application he dont feel the new state (that he has chm file)
now how could i remove the product file from programFiles.
Are you sure the exe file is the same?
It seems that either the help file is not found by your application or that the current security settings of IE prevent it from being loaded.
Use a tool like Procmon to see where your application is looking for the help file.
If it is not found make sure that it is included within your installer package.
If it is found make sure that it is on the local disc and that the default security settings for your local machine have not been changed within IE.

Categories