Form1.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Net;
  17. using System.IO.Compression;
  18. namespace QGIS_Installer
  19. {
  20. public partial class Form1 : Form
  21. {
  22. List<RegistryKey> list = new List<RegistryKey>();
  23. RegistryKey key3_4;
  24. RegistryKey key3_0;
  25. RegistryKey key3_6;
  26. RegistryKey key3_10;
  27. RegistryKey key3_8;
  28. RegistryKey key3_12;
  29. public Form1()
  30. {
  31. InitializeComponent();
  32. }
  33. private void copyFiles(string SourcePath, string DestinationPath)
  34. {
  35. // string dest = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  36. // string SourcePath = "C:\\Users\\Honza\\Documents\\testcopy\\folder1";
  37. // string DestinationPath = "C:\\Users\\Honza\\Documents\\testcopy\\folder2";
  38. try
  39. {
  40. foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
  41. SearchOption.AllDirectories))
  42. Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
  43. // Copy all the files &Replaces any files with the same name
  44. foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
  45. SearchOption.AllDirectories))
  46. File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
  47. }
  48. catch (Exception ex) //just for demonstration...it's always best to handle specific exceptions
  49. {
  50. //label1.ForeColor = System.Drawing.Color.Red;
  51. label1.Text = "You must run program as administrator!";
  52. }
  53. }
  54. private void button3_Click(object sender, EventArgs e)
  55. {
  56. RegistryKey key ;
  57. String s = comboBox1.Text;
  58. if (s == "QGIS 3_4")
  59. key = key3_4;
  60. else if (s == "QGIS 3_10")
  61. key = key3_10;
  62. else if (s == "QGIS 3_0")
  63. key = key3_0;
  64. else if (s == "QGIS 3_8")
  65. key = key3_8;
  66. else if (s == "QGIS 3_12")
  67. key = key3_12;
  68. else
  69. key = null;
  70. label1.Text = "Installing";
  71. downloadPlugin(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
  72. //using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
  73. // //using (var key = hklm.OpenSubKey(@"Software\\QGIS 3.4"))
  74. // foreach (RegistryKey key in list)
  75. // {
  76. string o = key.GetValue("InstallPath").ToString();
  77. //o = o + "\\apps\\Python37\\Lib\\site-packages"
  78. string out1 = o + "\\apps\\Python37\\Lib\\site-packages";
  79. string out2 = o + "\\apps\\Python37\\Scripts\\flask.exe";
  80. string out3 = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas";
  81. string input = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\packages";
  82. copyFiles(input, out1.ToString());
  83. string input2 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\flask.exe";
  84. try
  85. {
  86. File.Copy(input2, out2);
  87. }
  88. catch (Exception ex)
  89. {
  90. label1.ForeColor = System.Drawing.Color.Red;
  91. label1.Text = "You must run program as administrator!";
  92. }
  93. if (isPluginInstalled(o))
  94. {
  95. string input3 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\atlas";
  96. copyFiles(input3, out3);
  97. }
  98. else
  99. {
  100. if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins"))
  101. {
  102. label1.Text = " is ready!";
  103. Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins");
  104. }
  105. string input3 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\atlas";
  106. copyFiles(input3, out3);
  107. }
  108. //}
  109. checkStatus();
  110. if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed")
  111. {
  112. label1.ForeColor = System.Drawing.Color.Green;
  113. label1.Text = "Plugin is ready!";
  114. }
  115. else
  116. {
  117. label1.Text = "You must run program as administrator!";
  118. }
  119. }
  120. private void label1_Click(object sender, EventArgs e)
  121. {
  122. }
  123. private void Form1_Load(object sender, EventArgs e)
  124. {
  125. getVersions();
  126. label1.Text = "";
  127. // checkStatus();
  128. if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed")
  129. {
  130. // label1.ForeColor = System.Drawing.Color.Green;
  131. label1.Text = "Plugin is already installed.";
  132. }
  133. else
  134. {
  135. label1.Text = "Plugin is not installed.";
  136. }
  137. if (list.Count == 0)
  138. {
  139. button3.Enabled = false;
  140. label1.Text = "QGIS is not installed.";
  141. }
  142. }
  143. private void downloadPlugin(string path)
  144. {
  145. WebClient Client = new WebClient();
  146. Client.DownloadFile("https://gitlab.com/Vrobel/atlas/-/archive/master/atlas-master.zip", path + "\\atlas.zip");
  147. try { Directory.Delete(path + "\\atlas", true); }
  148. catch { }
  149. ZipFile.ExtractToDirectory(path + "\\atlas.zip", path);
  150. Directory.Move(path + "\\atlas-master", path + "\\atlas");
  151. try { Directory.Delete(path + "\\atlas-master", true); }
  152. catch { }
  153. }
  154. private bool isPluginInstalled(string o)
  155. {
  156. string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
  157. string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
  158. if (File.Exists(plugin) || File.Exists(plugin2))
  159. {
  160. return true;
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. private void label9_Click(object sender, EventArgs e)
  168. {
  169. }
  170. private void checkStatus()
  171. {
  172. using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
  173. using (var key = hklm.OpenSubKey(@"Software\\QGIS 3.4"))
  174. {
  175. string o = key.GetValue("InstallPath").ToString();
  176. string wergzeug = o + @"\apps\Python37\Lib\site-packages\werkzeug\test.py";
  177. if (File.Exists(wergzeug))
  178. {
  179. label2.Text = "Installed";
  180. }
  181. else
  182. {
  183. label2.Text = "Not Installed";
  184. }
  185. string flask = o + @"\apps\Python37\Lib\site-packages\flask\__init__.py";
  186. string flask2 = o + @"\apps\Python37\Scripts\flask.exe";
  187. if (File.Exists(flask) && File.Exists(flask2))
  188. {
  189. label3.Text = "Installed";
  190. }
  191. else
  192. {
  193. label3.Text = "Not Installed";
  194. }
  195. string click = o + @"\apps\Python37\Lib\site-packages\click\__init__.py";
  196. if (File.Exists(click))
  197. {
  198. label4.Text = "Installed";
  199. }
  200. else
  201. {
  202. label4.Text = "Not Installed";
  203. }
  204. string dangerous = o + @"\apps\Python37\Lib\site-packages\itsdangerous\__init__.py";
  205. if (File.Exists(dangerous))
  206. {
  207. label5.Text = "Installed";
  208. }
  209. else
  210. {
  211. label5.Text = "Not Installed";
  212. }
  213. string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
  214. string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
  215. if (File.Exists(plugin) || File.Exists(plugin2))
  216. {
  217. label6.Text = "Installed";
  218. }
  219. else
  220. {
  221. label6.Text = "Not Installed";
  222. }
  223. }
  224. }
  225. private void checkStatus(RegistryKey key)
  226. {
  227. string o = key.GetValue("InstallPath").ToString();
  228. string wergzeug = o + @"\apps\Python37\Lib\site-packages\werkzeug\test.py";
  229. if (File.Exists(wergzeug))
  230. {
  231. label2.Text = "Installed";
  232. }
  233. else
  234. {
  235. label2.Text = "Not Installed";
  236. }
  237. string flask = o + @"\apps\Python37\Lib\site-packages\flask\__init__.py";
  238. string flask2 = o + @"\apps\Python37\Scripts\flask.exe";
  239. if (File.Exists(flask) && File.Exists(flask2))
  240. {
  241. label3.Text = "Installed";
  242. }
  243. else
  244. {
  245. label3.Text = "Not Installed";
  246. }
  247. string click = o + @"\apps\Python37\Lib\site-packages\click\__init__.py";
  248. if (File.Exists(click))
  249. {
  250. label4.Text = "Installed";
  251. }
  252. else
  253. {
  254. label4.Text = "Not Installed";
  255. }
  256. string dangerous = o + @"\apps\Python37\Lib\site-packages\itsdangerous\__init__.py";
  257. if (File.Exists(dangerous))
  258. {
  259. label5.Text = "Installed";
  260. }
  261. else
  262. {
  263. label5.Text = "Not Installed";
  264. }
  265. string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
  266. string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
  267. if (File.Exists(plugin) || File.Exists(plugin2))
  268. {
  269. label6.Text = "Installed";
  270. }
  271. else
  272. {
  273. label6.Text = "Not Installed";
  274. }
  275. }
  276. private void button1_Click(object sender, EventArgs e)
  277. {
  278. string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  279. downloadPlugin(path);
  280. }
  281. private void label12_Click(object sender, EventArgs e)
  282. {
  283. }
  284. // private void button2_Click(object sender, EventArgs e)
  285. private void getVersions()
  286. {
  287. using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) {
  288. key3_4 = hklm.OpenSubKey(@"Software\\QGIS 3.4");
  289. key3_0 = hklm.OpenSubKey(@"Software\\QGIS 3.0");
  290. key3_6 = hklm.OpenSubKey(@"Software\\QGIS 3.6");
  291. key3_10 = hklm.OpenSubKey(@"Software\\QGIS 3.10");
  292. key3_8 = hklm.OpenSubKey(@"Software\\QGIS 3.8");
  293. key3_12 = hklm.OpenSubKey(@"Software\\QGIS 3.12");
  294. if (key3_4 != null) {
  295. list.Add(key3_4);
  296. label13.Text = "QGIS 3_4 detected \n";
  297. comboBox1.Items.Add("QGIS 3_4");
  298. comboBox1.Text = "QGIS 3_4";
  299. // checkStatus(key3_4);
  300. }
  301. if (key3_0 != null)
  302. {
  303. list.Add(key3_0);
  304. label13.Text = label13.Text + "QGIS 3_0 detected \n";
  305. comboBox1.Items.Add("QGIS 3_0");
  306. comboBox1.Text = "QGIS 3_0";
  307. // checkStatus(key3_0);
  308. }
  309. if (key3_6 != null)
  310. {
  311. list.Add(key3_6);
  312. label13.Text = label13.Text + "QGIS 3_6 detected \n";
  313. comboBox1.Items.Add("QGIS 3_6");
  314. comboBox1.Text = "QGIS 3_6";
  315. // checkStatus(key3_6);
  316. }
  317. if (key3_10 != null)
  318. {
  319. list.Add(key3_10);
  320. label13.Text = label13.Text + "QGIS 3_10 detected \n";
  321. comboBox1.Items.Add("QGIS 3_10");
  322. comboBox1.Text = "QGIS 3_10";
  323. // checkStatus(key3_10);
  324. }
  325. if (key3_8 != null) {
  326. list.Add(key3_8);
  327. label13.Text = label13.Text + "QGIS 3_8 detected \n";
  328. comboBox1.Items.Add("QGIS 3_8");
  329. comboBox1.Text = "QGIS 3_8";
  330. // checkStatus(key3_8);
  331. }
  332. if (key3_12 != null)
  333. {
  334. list.Add(key3_8);
  335. label13.Text = label13.Text + "QGIS 3_12 detected \n";
  336. comboBox1.Items.Add("QGIS 3_12");
  337. comboBox1.Text = "QGIS 3_12";
  338. // checkStatus(key3_12);
  339. }
  340. }
  341. }
  342. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  343. {
  344. String s = comboBox1.Text;
  345. if (s == "QGIS 3_4")
  346. checkStatus(key3_4);
  347. if (s == "QGIS 3_10")
  348. checkStatus(key3_10);
  349. if (s == "QGIS 3_0")
  350. checkStatus(key3_0);
  351. if (s == "QGIS 3_8")
  352. checkStatus(key3_8);
  353. if (s == "QGIS 3_12")
  354. checkStatus(key3_12);
  355. if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed") {
  356. // label1.ForeColor = System.Drawing.Color.Green;
  357. label1.Text = "Plugin is already installed.";
  358. }
  359. else {
  360. label1.Text = "Plugin is not installed.";
  361. }
  362. }
  363. }
  364. }