Form1.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. RegistryKey qgisKey = null;
  110. var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
  111. string selectedKey = comboBox1.SelectedItem.ToString();
  112. if (selectedKey == "QGIS 3_0")
  113. {
  114. qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.0");
  115. }
  116. if (selectedKey == "QGIS 3_4")
  117. {
  118. qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.4");
  119. }
  120. if (selectedKey == "QGIS 3_6")
  121. {
  122. qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.6");
  123. }
  124. if (selectedKey == "QGIS 3_8")
  125. {
  126. qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.8");
  127. }
  128. if (selectedKey == "QGIS 3_10")
  129. {
  130. qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.10");
  131. }
  132. if (selectedKey == "QGIS 3_12")
  133. {
  134. qgisKey = hklm.OpenSubKey(@"Software\\QGIS 3.12");
  135. }
  136. checkStatus(qgisKey);
  137. if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed")
  138. {
  139. label1.ForeColor = System.Drawing.Color.Green;
  140. label1.Text = "Plugin is ready!";
  141. }
  142. else
  143. {
  144. label1.Text = "You must run program as administrator!";
  145. }
  146. }
  147. private void label1_Click(object sender, EventArgs e)
  148. {
  149. }
  150. private void Form1_Load(object sender, EventArgs e)
  151. {
  152. getVersions();
  153. label1.Text = "";
  154. // checkStatus();
  155. if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed")
  156. {
  157. // label1.ForeColor = System.Drawing.Color.Green;
  158. label1.Text = "Plugin is already installed.";
  159. }
  160. else
  161. {
  162. label1.Text = "Plugin is not installed.";
  163. }
  164. if (list.Count == 0)
  165. {
  166. button3.Enabled = false;
  167. label1.Text = "QGIS is not installed.";
  168. }
  169. }
  170. private void downloadPlugin(string path)
  171. {
  172. WebClient Client = new WebClient();
  173. Client.DownloadFile("https://gitlab.com/Vrobel/atlas/-/archive/master/atlas-master.zip", path + "\\atlas.zip");
  174. try { Directory.Delete(path + "\\atlas", true); }
  175. catch { }
  176. ZipFile.ExtractToDirectory(path + "\\atlas.zip", path);
  177. Directory.Move(path + "\\atlas-master", path + "\\atlas");
  178. try { Directory.Delete(path + "\\atlas-master", true); }
  179. catch { }
  180. }
  181. private bool isPluginInstalled(string o)
  182. {
  183. string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
  184. string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
  185. if (File.Exists(plugin) || File.Exists(plugin2))
  186. {
  187. return true;
  188. }
  189. else
  190. {
  191. return false;
  192. }
  193. }
  194. private void label9_Click(object sender, EventArgs e)
  195. {
  196. }
  197. private void checkStatus()
  198. {
  199. using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
  200. using (var key = hklm.OpenSubKey(@"Software\\QGIS 3.4"))
  201. {
  202. string o = key.GetValue("InstallPath").ToString();
  203. string wergzeug = o + @"\apps\Python37\Lib\site-packages\werkzeug\test.py";
  204. if (File.Exists(wergzeug))
  205. {
  206. label2.Text = "Installed";
  207. }
  208. else
  209. {
  210. label2.Text = "Not Installed";
  211. }
  212. string flask = o + @"\apps\Python37\Lib\site-packages\flask\__init__.py";
  213. string flask2 = o + @"\apps\Python37\Scripts\flask.exe";
  214. if (File.Exists(flask) && File.Exists(flask2))
  215. {
  216. label3.Text = "Installed";
  217. }
  218. else
  219. {
  220. label3.Text = "Not Installed";
  221. }
  222. string click = o + @"\apps\Python37\Lib\site-packages\click\__init__.py";
  223. if (File.Exists(click))
  224. {
  225. label4.Text = "Installed";
  226. }
  227. else
  228. {
  229. label4.Text = "Not Installed";
  230. }
  231. string dangerous = o + @"\apps\Python37\Lib\site-packages\itsdangerous\__init__.py";
  232. if (File.Exists(dangerous))
  233. {
  234. label5.Text = "Installed";
  235. }
  236. else
  237. {
  238. label5.Text = "Not Installed";
  239. }
  240. string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
  241. string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
  242. if (File.Exists(plugin) || File.Exists(plugin2))
  243. {
  244. label6.Text = "Installed";
  245. }
  246. else
  247. {
  248. label6.Text = "Not Installed";
  249. }
  250. }
  251. }
  252. private void checkStatus(RegistryKey key)
  253. {
  254. string o = key.GetValue("InstallPath").ToString();
  255. string wergzeug = o + @"\apps\Python37\Lib\site-packages\werkzeug\test.py";
  256. if (File.Exists(wergzeug))
  257. {
  258. label2.Text = "Installed";
  259. }
  260. else
  261. {
  262. label2.Text = "Not Installed";
  263. }
  264. string flask = o + @"\apps\Python37\Lib\site-packages\flask\__init__.py";
  265. string flask2 = o + @"\apps\Python37\Scripts\flask.exe";
  266. if (File.Exists(flask) && File.Exists(flask2))
  267. {
  268. label3.Text = "Installed";
  269. }
  270. else
  271. {
  272. label3.Text = "Not Installed";
  273. }
  274. string click = o + @"\apps\Python37\Lib\site-packages\click\__init__.py";
  275. if (File.Exists(click))
  276. {
  277. label4.Text = "Installed";
  278. }
  279. else
  280. {
  281. label4.Text = "Not Installed";
  282. }
  283. string dangerous = o + @"\apps\Python37\Lib\site-packages\itsdangerous\__init__.py";
  284. if (File.Exists(dangerous))
  285. {
  286. label5.Text = "Installed";
  287. }
  288. else
  289. {
  290. label5.Text = "Not Installed";
  291. }
  292. string plugin = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\atlas\\__init__.py";
  293. string plugin2 = o + @"\apps\qgis-ltr\python\plugins\atlas\__init__.py";
  294. if (File.Exists(plugin) || File.Exists(plugin2))
  295. {
  296. label6.Text = "Installed";
  297. }
  298. else
  299. {
  300. label6.Text = "Not Installed";
  301. }
  302. }
  303. private void button1_Click(object sender, EventArgs e)
  304. {
  305. string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  306. downloadPlugin(path);
  307. }
  308. private void label12_Click(object sender, EventArgs e)
  309. {
  310. }
  311. // private void button2_Click(object sender, EventArgs e)
  312. private void getVersions()
  313. {
  314. using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) {
  315. key3_4 = hklm.OpenSubKey(@"Software\\QGIS 3.4");
  316. key3_0 = hklm.OpenSubKey(@"Software\\QGIS 3.0");
  317. key3_6 = hklm.OpenSubKey(@"Software\\QGIS 3.6");
  318. key3_10 = hklm.OpenSubKey(@"Software\\QGIS 3.10");
  319. key3_8 = hklm.OpenSubKey(@"Software\\QGIS 3.8");
  320. key3_12 = hklm.OpenSubKey(@"Software\\QGIS 3.12");
  321. if (key3_4 != null) {
  322. list.Add(key3_4);
  323. label13.Text = "QGIS 3_4 detected \n";
  324. comboBox1.Items.Add("QGIS 3_4");
  325. comboBox1.Text = "QGIS 3_4";
  326. // checkStatus(key3_4);
  327. }
  328. if (key3_0 != null)
  329. {
  330. list.Add(key3_0);
  331. label13.Text = label13.Text + "QGIS 3_0 detected \n";
  332. comboBox1.Items.Add("QGIS 3_0");
  333. comboBox1.Text = "QGIS 3_0";
  334. // checkStatus(key3_0);
  335. }
  336. if (key3_6 != null)
  337. {
  338. list.Add(key3_6);
  339. label13.Text = label13.Text + "QGIS 3_6 detected \n";
  340. comboBox1.Items.Add("QGIS 3_6");
  341. comboBox1.Text = "QGIS 3_6";
  342. // checkStatus(key3_6);
  343. }
  344. if (key3_10 != null)
  345. {
  346. list.Add(key3_10);
  347. label13.Text = label13.Text + "QGIS 3_10 detected \n";
  348. comboBox1.Items.Add("QGIS 3_10");
  349. comboBox1.Text = "QGIS 3_10";
  350. // checkStatus(key3_10);
  351. }
  352. if (key3_8 != null) {
  353. list.Add(key3_8);
  354. label13.Text = label13.Text + "QGIS 3_8 detected \n";
  355. comboBox1.Items.Add("QGIS 3_8");
  356. comboBox1.Text = "QGIS 3_8";
  357. // checkStatus(key3_8);
  358. }
  359. if (key3_12 != null)
  360. {
  361. list.Add(key3_8);
  362. label13.Text = label13.Text + "QGIS 3_12 detected \n";
  363. comboBox1.Items.Add("QGIS 3_12");
  364. comboBox1.Text = "QGIS 3_12";
  365. // checkStatus(key3_12);
  366. }
  367. }
  368. }
  369. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  370. {
  371. String s = comboBox1.Text;
  372. if (s == "QGIS 3_4")
  373. checkStatus(key3_4);
  374. if (s == "QGIS 3_10")
  375. checkStatus(key3_10);
  376. if (s == "QGIS 3_0")
  377. checkStatus(key3_0);
  378. if (s == "QGIS 3_8")
  379. checkStatus(key3_8);
  380. if (s == "QGIS 3_12")
  381. checkStatus(key3_12);
  382. if (label2.Text == "Installed" && label3.Text == "Installed" && label4.Text == "Installed" && label5.Text == "Installed" && label6.Text == "Installed") {
  383. // label1.ForeColor = System.Drawing.Color.Green;
  384. label1.Text = "Plugin is already installed.";
  385. }
  386. else {
  387. label1.Text = "Plugin is not installed.";
  388. }
  389. }
  390. }
  391. }