Sfall-ScriptEditor
108 строк · 4.6 Кб
1using System;2using System.Collections.Generic;3using System.Text;4using Microsoft.Win32;5using System.Runtime.InteropServices;6using System.Windows.Forms;7using System.IO;8
9namespace ScriptEditor10{
11public static class FileAssociation12{13[DllImport("shell32.dll", SetLastError = true)]14private static extern void SHChangeNotify(int wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);15
16private const string FILE_EXTENSION = ".ssl";17private const int SHCNE_ASSOCCHANGED = 0x8000000;18private const uint SHCNF_IDLIST = 0x0U;19
20private static readonly string appName = "SfallScriptEditor";21private static readonly string[] extAllowed = { FILE_EXTENSION, ".msg", ".int", ".fcd", ".h", ".ini", ".txt", ".cfg", ".xshd" };22
23public static bool CheckFileAllow(string ext, out bool Exists)24{25if (File.Exists(ext))26Exists = true;27else28Exists = false;29ext = Path.GetExtension(ext).ToLowerInvariant();30bool result = (Array.IndexOf(extAllowed, ext) > -1);31if (!result)32MessageBox.Show("You can not open this file type in the editor.", "Error - file is not allowed");33return result;34}35
36public static void Associate(bool force = false)37{38if (!force && IsAssociated)39return;40
41if (MessageBox.Show("Do you want to associate the files (.ssl .int and .msg) to the script editor?",42"Associate files", MessageBoxButtons.YesNo) == DialogResult.No)43return;44
45for (int i = 0; i < 4; i++)46{47// Удалить ранее ассоциированные с файлом разделы48var value = Registry.ClassesRoot.CreateSubKey(extAllowed[i]).GetValue("");49if (value != null)50Registry.ClassesRoot.DeleteSubKeyTree(value.ToString(), false);51
52// Создаем новый раздел53Registry.ClassesRoot.CreateSubKey(extAllowed[i]).SetValue("", appName + extAllowed[i].Remove(0,1).ToUpper());54using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(appName + extAllowed[i].Remove(0, 1).ToUpper()))55{56key.SetValue("", "Sfall Script Editor v.4.0");57key.SetValue("AlwaysShowExt", "");58key.CreateSubKey("DefaultIcon").SetValue("", Settings.ResourcesFolder + "\\icon_" + extAllowed[i].Remove(0,1) + ".ico");59key.CreateSubKey("Shell").SetValue("", "OpenSSEditor");60key.CreateSubKey(@"Shell\OpenSSEditor").SetValue("", "Open in Sfall ScriptEditor");61key.CreateSubKey(@"Shell\OpenSSEditor\Command").SetValue("", Application.ExecutablePath + " \"%1\"");62}63}64SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);65}66
67private static bool IsAssociated68{69get {70string value = "";71var reg = Registry.ClassesRoot.OpenSubKey(FILE_EXTENSION, false);72if (reg != null)73value = reg.GetValue("", string.Empty).ToString();74return (value == (appName + "SSL"));75}76}77
78public static bool CheckFCDFile(ref string file)79{80string fltmp = file;81if (Path.GetExtension(fltmp) == ".fcd") {82fltmp = Path.ChangeExtension(fltmp, ".ssl");83if (File.Exists(fltmp)){84file = fltmp;85return true;86} else {87int z = fltmp.LastIndexOf(Path.DirectorySeparatorChar);88if (z > 0) {89z = fltmp.LastIndexOf(Path.DirectorySeparatorChar, z - 1);90if (z > 0) {91string path = fltmp.Remove(z + 1);92fltmp = Path.Combine(path, Path.GetFileName(fltmp));93if (File.Exists(fltmp)) {94file = fltmp;95return true;96}97}98}99}100file = null;101}102if (file == null)103MessageBox.Show("The script file for this flowchart was not found.", "Missing script file");104
105return false;106}107}108}
109