Sfall-ScriptEditor

Форк
0
/
FileAssociation.cs 
108 строк · 4.6 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using Microsoft.Win32;
5
using System.Runtime.InteropServices;
6
using System.Windows.Forms;
7
using System.IO;
8

9
namespace ScriptEditor
10
{
11
    public static class FileAssociation
12
    {
13
        [DllImport("shell32.dll", SetLastError = true)]
14
        private static extern void SHChangeNotify(int wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
15

16
        private const string FILE_EXTENSION = ".ssl";
17
        private const int SHCNE_ASSOCCHANGED = 0x8000000;
18
        private const uint SHCNF_IDLIST = 0x0U;
19

20
        private static readonly string appName = "SfallScriptEditor";
21
        private static readonly string[] extAllowed = { FILE_EXTENSION, ".msg", ".int", ".fcd", ".h", ".ini", ".txt", ".cfg", ".xshd" };
22

23
        public static bool CheckFileAllow(string ext, out bool Exists)
24
        {
25
            if (File.Exists(ext))
26
                Exists = true;
27
            else 
28
                Exists = false;
29
            ext = Path.GetExtension(ext).ToLowerInvariant();
30
            bool result = (Array.IndexOf(extAllowed, ext) > -1);
31
            if (!result)
32
                MessageBox.Show("You can not open this file type in the editor.", "Error - file is not allowed");
33
            return result;
34
        }
35

36
        public static void Associate(bool force = false)
37
        {
38
            if (!force && IsAssociated)
39
                return; 
40
  
41
            if (MessageBox.Show("Do you want to associate the files (.ssl .int and .msg) to the script editor?",
42
                "Associate files", MessageBoxButtons.YesNo) == DialogResult.No)
43
                return;
44

45
            for (int i = 0; i < 4; i++)
46
            {
47
                // Удалить ранее ассоциированные с файлом разделы
48
                var value = Registry.ClassesRoot.CreateSubKey(extAllowed[i]).GetValue("");
49
                if (value != null)
50
                    Registry.ClassesRoot.DeleteSubKeyTree(value.ToString(), false);
51
                
52
                // Создаем новый раздел
53
                Registry.ClassesRoot.CreateSubKey(extAllowed[i]).SetValue("", appName + extAllowed[i].Remove(0,1).ToUpper());
54
                using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(appName + extAllowed[i].Remove(0, 1).ToUpper()))
55
                {
56
                    key.SetValue("", "Sfall Script Editor v.4.0");
57
                    key.SetValue("AlwaysShowExt", "");
58
                    key.CreateSubKey("DefaultIcon").SetValue("", Settings.ResourcesFolder + "\\icon_" + extAllowed[i].Remove(0,1) + ".ico");
59
                    key.CreateSubKey("Shell").SetValue("", "OpenSSEditor");
60
                    key.CreateSubKey(@"Shell\OpenSSEditor").SetValue("", "Open in Sfall ScriptEditor");
61
                    key.CreateSubKey(@"Shell\OpenSSEditor\Command").SetValue("", Application.ExecutablePath + " \"%1\"");
62
                }
63
            }
64
            SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
65
        }
66

67
        private static bool IsAssociated
68
        {
69
            get {
70
                string value = "";
71
                var reg = Registry.ClassesRoot.OpenSubKey(FILE_EXTENSION, false);
72
                if (reg != null)
73
                     value = reg.GetValue("", string.Empty).ToString();  
74
                return (value == (appName + "SSL"));
75
            }
76
        }
77

78
        public static bool CheckFCDFile(ref string file)
79
        {
80
            string fltmp = file;
81
            if (Path.GetExtension(fltmp) == ".fcd") {
82
                    fltmp = Path.ChangeExtension(fltmp, ".ssl");
83
                    if (File.Exists(fltmp)){
84
                        file = fltmp;
85
                        return true;
86
                    } else {
87
                        int z = fltmp.LastIndexOf(Path.DirectorySeparatorChar);
88
                        if (z > 0) {
89
                            z = fltmp.LastIndexOf(Path.DirectorySeparatorChar, z - 1);
90
                            if (z > 0) {
91
                                string path = fltmp.Remove(z + 1);
92
                                fltmp = Path.Combine(path, Path.GetFileName(fltmp));
93
                                if (File.Exists(fltmp)) {
94
                                    file = fltmp;
95
                                    return true;
96
                                }
97
                            }
98
                        }
99
                    }
100
                    file = null;
101
            }
102
            if (file == null)
103
                MessageBox.Show("The script file for this flowchart was not found.", "Missing script file");
104

105
            return false;
106
        }
107
    }
108
}
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.