File extension file association and ClickOnce - c#

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

Related

Different application settings per executable

I'm creating an application and I make use of Properties.Settings to store the settings.
However, let's say my application is on the desktop of the user and is called Program.exe, now, when the user copies this executable and places it somewhere else or even renames it, all settings are gone.
Why is C# doing this? Is there any way this can be turned off while pertaining the user scope? I don't wish to use the application scope since multiple users can be sharing the same computer.
The settings are likely stored in {appname}.exe.config which apparently is not being copied/renamed with the executable. Either copy the executable with the file, hard-code them in the application, or find another mechanism to get/set app settings (like the registry).
The application will look for these settings in a file titled {appname}.exe.config. If the executable is renamed (without renaming the .config file) or copied to another location without copying the .config file along with it, the application won't know where to look for their settings so they will be blank (unless you set a default value in the app).
You can "hard-code" default settings by putting a value in the Settings.settings "file" in Visual studio (which effectively adds the default value as an attribute to the setting).
Another option would be to hard-code the values directly in the source code. I'm NOT recommending this approach as it hinders the ability to change that value, but if you want to be able to deploy the app by just copying the EXE (and nothing else) then it is an option.
See http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx for details.
The settings (and their location) when using Properties.Settings is determined by the Scope setting of the particular setting.
Settings that are Application scoped are in the app.config file (renamed at compile time to .exe.config - these settings are read only at run time), and settings that are User scoped are saved in a user.config that is tied to the user's profile on the machine (these settings are read/write at run time). By properly scoping your settings you should be able to avoid this type of problem.
Settings are stored in the user profile (under C:\Users[UserName]\AppData\Local), these settings are under folders that have the application name and also have an identifier of the location of the exe file they refer to, for example: MyApp.exe_Url_hpvvra0rj4y03ebpz3cfmzsrcpczat11, refers to the executable under Program Files, if i move or copy the exe and run it, it will create another folder with another URL (MyApp.exe_Url_vqzsq0spwewydv3wrnebtqji24nwuboe, for example), i haven't find a way to avoid this but a good workaround is to copy the settings from another config file, just note that this method is not foolproof as you have to choose the right file to copy the setting to and if you expect your exe to be copied or moved constantly this will be a hard task.
Another workaround is to simply create your custom settings class and put the file anywhere your application can find it and don't realy on the built in settings on .NET.

Opening files from Windows Explorer in my Metro App

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
}

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

Link to config file in .Net setup project

I am creating an installer for my C# application, and I want to put a link to the configuration file into the start menu, so that users can open it for editing in notepad from the program folder in the start menu.
I seem not to be able to put a link to it however - does anyone know how to do this? (Really, I would just love to put "[targetdir]\myapp.exe.config but VS doesn't let me edit the field, only select from a file browser).
Many thanks,
Rob
I found the solution and it's pretty easy:
Add Project Output for your project in the setup project
Select Project Output from File System in left pane and go to the properties of the project output. Then add a filter *.config to remove the .config file from your project output.
Rename the app.config from your actual project to the output name that your App exe ([ProductName]) will have along with the config extension ofcourse.
Add the [ProductName].config to your setup project as a File.
Create a Shortcut to that file and add it in any setup folder desktop or program folder.
Voila.
You're all set.
Isn't the config file added in your installer? You should be able to select it when prompted for the shortcut target (the "Select Item in Project" dialog). Please note that in this prompt dialog you first need to browse to the folder which contains it (for example double-click "Application Folder").
If the config file is not added, you need to manually add it in "Application Folder". Only then you can create a shortcut to it.
Please note that Visual Studio doesn't support shortcuts to a specific file from a project output which generates multiple files. In this case you can try using a custom action which creates the shortcut through custom code.
I remember doing it in Vs2005 using as below:
File System Editor > Users Programs Menu
Add> Folder
Add file (Say Config file) point it to the its location

ClickOnce configuration deployment - configuration files

I'm deploying an application using ClickOnce, the problem is that the configuration file (xxx.exe.config) is not embedded in the package and there is no option to include it. Another problem with the config is that When I'm trying to manually write it to the directory where it is delopyed (Environment.CurrentDirectory) I'm getting an exception - I have no permissions to do that.
Any ideas on how to deploy the app along with the configuration file? (and to make it writable, because the applications during ti's runtime alters the config values.)
Thanks in advance
You can also change build action for files that you want to include in ClickOnce deploy: Properties for a file → Build Action → set to "Content", this will add the file to the list of Application files in Publish project options.
If you need to publish a file from a referenced project, I didn't find a better solution rather than to Add → Existing Item → On "Add" button, select "Add as Link", then set action to Content and check the list of published files.
The config file should get published automatically; if not, ensure that it is configured to copy to output, and (if that fails) check the publish files (project properties -> publish -> application files; the config file should be marked as "include (auto)" or "include").
You shouldn't attempt to update anything in the app install directory. That is a bad idea generally (since you can't assume you can update "program files" unless you're an admin), but the same holds true for ClickOnce too.
Just create a settings file with some user settings; these will be saved in the user's profile, so can be updated reliably. You can't edit the files deployed via ClickOnce; even if you had access, it would (by default) break the hashing function, and it will refuse to load them. You can turn off the hashing, but... this still isn't a great idea.
When you click on the Properties of your project and go to the Publish tab of the project's properties, click Application Files... then check the 'Show All Files' checkbox. You should see an option to choose your applications config file from there.
In my experience the <*>.exe.config file is usually set to include automatically however.

Categories