Changeset 13 for Common/ExtTools.cs


Ignore:
Timestamp:
Feb 8, 2021, 4:23:17 PM (3 years ago)
Author:
chronos
Message:
  • Modified: Fixes to existing classes.
  • Added: File type association method.
  • Added: RecentFiles and Table classes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/ExtTools.cs

    r8 r13  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
     1using System;
    62using System.IO;
    73using System.Diagnostics;
    84using System.Windows.Forms;
     5using Microsoft.Win32;
     6using System.Runtime.InteropServices;
    97
    108namespace Common
     
    1210    static class ExtTools
    1311    {
    14         public static string compareProgram;
    15         public static string compareAttributes;
     12        public static string CompareProgram;
     13        public static string CompareAttributes;
    1614
    1715        static ExtTools()
    1816        {
    1917            // Autodetect location of Beyond Compare executable
    20             compareProgram = "C:\\Program Files\\Beyond Compare 4\\BCompare.exe";
    21             if (!File.Exists(compareProgram)) compareProgram = "C:\\Program Files (x86)\\Beyond Compare 4\\BCompare.exe";
    22             if (!File.Exists(compareProgram)) compareProgram = "C:\\Program Files\\Beyond Compare 3\\BCompare.exe";
    23             if (!File.Exists(compareProgram)) compareProgram = "C:\\Program Files (x86)\\Beyond Compare 3\\BCompare.exe";
     18            CompareProgram = "C:\\Program Files\\Beyond Compare 4\\BCompare.exe";
     19            if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files (x86)\\Beyond Compare 4\\BCompare.exe";
     20            if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files\\Beyond Compare 3\\BCompare.exe";
     21            if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files (x86)\\Beyond Compare 3\\BCompare.exe";
    2422
    25             compareAttributes = "\"<<left_file>>\" \"<<right_file>>\"";
     23            CompareAttributes = "\"<<left_file>>\" \"<<right_file>>\"";
    2624        }
    2725
     
    2927        {
    3028            return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" +
    31                 System.Windows.Forms.Application.ProductName + "\\Temp";
     29                Application.ProductName + "\\Temp";
    3230        }
    3331
     
    5149            {
    5250                // Execute comparison process
    53                 string atts = compareAttributes;
    54                 atts = atts.Replace("<<left_file>>", filenameLeft);
    55                 atts = atts.Replace("<<right_file>>", filenameRight);
    56                 ExecuteProgram(compareProgram, atts);
     51                string attributes = CompareAttributes;
     52                attributes = attributes.Replace("<<left_file>>", filenameLeft);
     53                attributes = attributes.Replace("<<right_file>>", filenameRight);
     54                ExecuteProgram(CompareProgram, attributes);
    5755            } catch (Exception e)
    5856            {
    59                 MessageBox.Show(e.Message.ToString());
     57                MessageBox.Show(e.Message);
    6058            }
    6159        }
    6260
    63         public static void ExecuteProgram(string executableFileName, string arguments, string workingDirectory = "")
     61        public static void ExecuteProgram(string executableFileName, string arguments = "", string workingDirectory = "")
    6462        {
    6563            // Execute external process
     
    106104            }
    107105            return output;
    108         }       
     106        }
     107
     108        public static void SetFileAssociation(string extension, string progId, string openWith, string fileDescription)
     109        {
     110            RegistryKey BaseKey;
     111            RegistryKey OpenMethod;
     112            RegistryKey Shell;
     113            RegistryKey CurrentUser;
     114
     115            BaseKey = Registry.ClassesRoot.CreateSubKey(extension);
     116            BaseKey?.SetValue("", progId);
     117
     118            OpenMethod = Registry.ClassesRoot.CreateSubKey(progId);
     119            OpenMethod?.SetValue("", fileDescription);
     120            OpenMethod?.CreateSubKey("DefaultIcon")?.SetValue("", "\"" + openWith + "\",0");
     121            Shell = OpenMethod?.CreateSubKey("Shell");
     122            Shell?.CreateSubKey("edit")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\"");
     123            Shell?.CreateSubKey("open")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\"");
     124            Shell?.Close();
     125            OpenMethod?.Close();
     126            BaseKey?.Close();
     127
     128            // Delete explorer override
     129            CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension, true);
     130            CurrentUser?.DeleteSubKey("UserChoice", false);
     131            CurrentUser?.Close();
     132
     133            // Tell explorer the file association has been changed
     134            SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
     135        }
     136
     137        [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
     138        public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
    109139    }
    110140}
Note: See TracChangeset for help on using the changeset viewer.