diff options
| author | Alex-TIMEHACK <[email protected]> | 2017-07-02 16:31:46 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-07-02 16:31:46 +0100 |
| commit | 3de51a2e1b9224e8a8355e3945e458e1651821a9 (patch) | |
| tree | ec0016f90351794301e8eda68e77fb9766f5f9fb /TimeHACK.Engine/FileAssociation.cs | |
| parent | 69ae842e6e3193a27b0294f92784ef97eb69fb37 (diff) | |
| parent | 99938de884fcf0dd572a330145dd91686268a45f (diff) | |
| download | histacom2-3de51a2e1b9224e8a8355e3945e458e1651821a9.tar.gz histacom2-3de51a2e1b9224e8a8355e3945e458e1651821a9.tar.bz2 histacom2-3de51a2e1b9224e8a8355e3945e458e1651821a9.zip | |
Merge pull request #108 from Alex-TIMEHACK/master
Made the Windows folder have SOMETHING in it
Diffstat (limited to 'TimeHACK.Engine/FileAssociation.cs')
| -rw-r--r-- | TimeHACK.Engine/FileAssociation.cs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/TimeHACK.Engine/FileAssociation.cs b/TimeHACK.Engine/FileAssociation.cs new file mode 100644 index 0000000..25fe895 --- /dev/null +++ b/TimeHACK.Engine/FileAssociation.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; +using Microsoft.Win32; + +namespace TimeHACK.Engine +{ + public class FileAssociation + { + // Associate file extension with progID, description, icon and application + public static void Associate(string extension, + string progID, string description, string icon, string application) + { + Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID); + if (progID != null && progID.Length > 0) + using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID)) + { + if (description != null) + key.SetValue("", description); + if (icon != null) + key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon)); + if (application != null) + key.CreateSubKey(@"Shell\Open\Command").SetValue("", + ToShortPathName(application) + " \"%1\""); + } + } + + // Return true if extension already associated in registry + public static bool IsAssociated(string extension) + { + return (Registry.ClassesRoot.OpenSubKey(extension, false) != null); + } + + [DllImport("Kernel32.dll")] + private static extern uint GetShortPathName(string lpszLongPath, + [Out] StringBuilder lpszShortPath, uint cchBuffer); + + // Return short path format of a file name + private static string ToShortPathName(string longName) + { + StringBuilder s = new StringBuilder(1000); + uint iSize = (uint)s.Capacity; + uint iRet = GetShortPathName(longName, s, iSize); + return s.ToString(); + } + } +} |
