Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.02 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using System.Threading;
  7. using Microsoft.VisualBasic;
  8.  
  9. namespace HololensAppInstallation
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public static Label statusLabel = new Label();
  14. public static Label errorTitleLabel = new Label();
  15. public static Label errorDescLabel = new Label();
  16.  
  17. public static Button submitButton;
  18. public static Button restartButton;
  19.  
  20. // If every entry is true, all threads are finished
  21. static List<bool> processesDone = new List<bool>();
  22. bool hasCheckingStarted = false;
  23.  
  24. static int topBorder = 25;
  25.  
  26. static Form form = new Form();
  27.  
  28. static Dictionary<String, String> dropdownStates = new Dictionary<String, String>();
  29.  
  30. public static List<CheckBox> checkboxesApps;
  31. static List<ComboBox> comboboxesApps;
  32. static int selectedAppCount = 0;
  33. List<Label> selectedAppList;
  34. String[] hololensAppPath;
  35. String desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  36. String[] completePath;
  37. public static String username = "";
  38. public static String password = "";
  39.  
  40. public Form1()
  41. {
  42. String rootFolder = @"M:\projekte\Muenchen_Projekte\Executables\HoloLens_Installation";
  43.  
  44. InitializeComponent();
  45. Paint += new PaintEventHandler(Form1_Paint);
  46. FormBorderStyle = FormBorderStyle.FixedSingle;
  47. MaximizeBox = false;
  48. MinimizeBox = false;
  49. Text = "HoloLens App Installation";
  50. BackColor = Color.White;
  51.  
  52. hololensAppPath = Directory.GetDirectories(rootFolder);
  53. selectedAppList = new List<Label>();
  54. checkboxesApps = new List<CheckBox>();
  55. comboboxesApps = new List<ComboBox>();
  56.  
  57. restartButton = new Button()
  58. {
  59. Size = new Size(35, 35),
  60. Top = 10,
  61. Left = 200,
  62. BackColor = Color.White,
  63. TextAlign = ContentAlignment.MiddleCenter
  64. };
  65.  
  66. object o = Properties.Res
  67. Image img = (Image)o;
  68. //restartButton.Image = Image.FromFile(@"C:\Users\mckn\Desktop\cancel_img.png");
  69. Controls.Add(restartButton);
  70.  
  71. statusLabel.Width = form.Width - 17;
  72. statusLabel.Height = 36;
  73. statusLabel.Top = 10;
  74. statusLabel.BackColor = Color.White;
  75. statusLabel.Text = "Done";
  76. statusLabel.ForeColor = Color.Gray;
  77. statusLabel.Font = new Font("Arial", 14);
  78. statusLabel.TextAlign = ContentAlignment.MiddleCenter;
  79. Controls.Add(statusLabel);
  80.  
  81. errorTitleLabel.Width = (form.Width - 16) / 3;
  82. errorTitleLabel.Height = 36;
  83. errorTitleLabel.Top = 10;
  84. errorTitleLabel.BackColor = Color.White;
  85. errorTitleLabel.Text = "Fehler!";
  86. errorTitleLabel.ForeColor = Color.Red;
  87. errorTitleLabel.Font = new Font("Arial", 16);
  88. errorTitleLabel.TextAlign = ContentAlignment.MiddleCenter;
  89. Controls.Add(errorTitleLabel);
  90. errorTitleLabel.Visible = false;
  91.  
  92. errorDescLabel.Width = Convert.ToInt32((form.Width - 16) / 1.5f);
  93. errorDescLabel.Height = 36;
  94. errorDescLabel.Top = 10;
  95. errorDescLabel.Left = errorTitleLabel.Width;
  96. errorDescLabel.BackColor = Color.White;
  97. errorDescLabel.Text = "";
  98. errorDescLabel.ForeColor = Color.Red;
  99. errorDescLabel.Font = new Font("Arial", 12);
  100. errorDescLabel.TextAlign = ContentAlignment.MiddleCenter;
  101. Controls.Add(errorDescLabel);
  102. errorDescLabel.Visible = false;
  103.  
  104.  
  105.  
  106. for (int i = 0; i < hololensAppPath.Length; i++)
  107. {
  108. // Wenn das array nicht leer ist, besteht ein subdirectory
  109. String[] possibleSubdirectories = Directory.GetDirectories(hololensAppPath[i]);
  110. if (possibleSubdirectories.Length > 0)
  111. {
  112. CheckBox tempCheckBox = new CheckBox()
  113. {
  114. Left = 25,
  115. Top = topBorder + ((i + 1) * 40) - 4,
  116. Parent = this,
  117. Name = "CheckBox " + (i + 1),
  118. };
  119.  
  120. for (int y = 0; y < possibleSubdirectories.Length; y++)
  121. {
  122. String[] substrings = possibleSubdirectories[y].Split('\\');
  123. possibleSubdirectories[y] = substrings[substrings.Length - 1];
  124. }
  125.  
  126. String[] appSurname = possibleSubdirectories[0].Split('_');
  127. String appName = appSurname[0];
  128.  
  129. for (int y = 0; y < possibleSubdirectories.Length; y++)
  130. {
  131. String[] substrings2 = possibleSubdirectories[y].Split('_');
  132. possibleSubdirectories[y] = substrings2[1];
  133. }
  134.  
  135. Label tempLabel = new Label()
  136. {
  137. Left = 50,
  138. Top = topBorder + (i + 1) * 40,
  139. Parent = this,
  140. Name = "Label " + (i + 1),
  141. Text = appName
  142. };
  143. tempLabel.AutoSize = true;
  144.  
  145. ComboBox tempComboBox = new ComboBox();
  146. tempComboBox.Items.AddRange(possibleSubdirectories);
  147. tempComboBox.Left = 70 + tempLabel.Width;
  148. tempComboBox.Top = topBorder + (i + 1) * 40;
  149. tempComboBox.Name = "ComboBox " + (i + 1);
  150. tempComboBox.MaxDropDownItems = possibleSubdirectories.Length;
  151. tempComboBox.TabIndex = 0;
  152. tempComboBox.SelectedIndex = 0;
  153. tempComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  154. tempComboBox.SelectedIndexChanged += new EventHandler(tempComboBox_SelectedIndexChanged);
  155. tempComboBoxInitialization(tempComboBox);
  156.  
  157. comboboxesApps.Add(tempComboBox);
  158. selectedAppList.Add(tempLabel);
  159. checkboxesApps.Add(tempCheckBox);
  160. Controls.Add(tempComboBox);
  161. Controls.Add(tempCheckBox);
  162. }
  163. else
  164. {
  165. String[] substrings = hololensAppPath[i].Split('\\');
  166. String appName = substrings[substrings.Length - 1];
  167.  
  168. Label tempLabel = new Label()
  169. {
  170. Left = 50,
  171. Top = topBorder + (i + 1) * 40,
  172. Parent = this,
  173. Name = "Label " + (i + 1),
  174. Text = appName
  175. };
  176. tempLabel.AutoSize = true;
  177.  
  178. CheckBox tempCheckBox = new CheckBox()
  179. {
  180. Left = 25,
  181. Top = topBorder + ((i + 1) * 40) - 4,
  182. Parent = this,
  183. Name = "CheckBox " + (i + 1),
  184. };
  185.  
  186. ComboBox tempComboBox = new ComboBox();
  187. comboboxesApps.Add(tempComboBox);
  188.  
  189. selectedAppList.Add(tempLabel);
  190. checkboxesApps.Add(tempCheckBox);
  191. Controls.Add(tempLabel);
  192. Controls.Add(tempCheckBox);
  193. }
  194. Height += 40;
  195. }
  196.  
  197. submitButton = new Button()
  198. {
  199. AutoSize = false,
  200. Width = 100,
  201. Text = "Installation starten"
  202. };
  203.  
  204. submitButton.Top = Height - 100;
  205. submitButton.Left = (Width - submitButton.Width) / 2;
  206. Controls.Add(submitButton);
  207. submitButton.Click += new EventHandler(submitButton_Click);
  208. }
  209.  
  210. private void submitButton_Click(object sender, EventArgs e)
  211. {
  212. statusLabel.Visible = true;
  213. errorTitleLabel.Visible = false;
  214. errorDescLabel.Visible = false;
  215. statusLabel.Text = "Installing";
  216. statusLabel.ForeColor = Color.Orange;
  217. Cursor = Cursors.WaitCursor;
  218. Directory.CreateDirectory(desktopPath + "\\tempFolder");
  219. for (int i = 0; i < checkboxesApps.Count; i++)
  220. {
  221. if (checkboxesApps[i].Checked)
  222. {
  223. selectedAppCount++;
  224. if (submitButton.Enabled)
  225. {
  226. submitButton.Enabled = false;
  227. foreach (CheckBox box in checkboxesApps)
  228. {
  229. box.Enabled = false;
  230. }
  231. foreach (ComboBox box in comboboxesApps)
  232. {
  233. box.Enabled = false;
  234. }
  235. }
  236.  
  237. completePath = Directory.GetFiles(hololensAppPath[i]);
  238. String[] newCompletePath = new String[1];
  239. // if there is no file, but there are directories
  240. if (completePath.Length == 0)
  241. {
  242. String[] directories = Directory.GetDirectories(hololensAppPath[i]);
  243. foreach (String s in directories)
  244. {
  245. if (s.Contains(dropdownStates[comboboxesApps[i].Name]))
  246. {
  247. newCompletePath = Directory.GetFiles(s);
  248. }
  249. }
  250. }
  251.  
  252. string copyToPath = desktopPath + "\\tempFolder" + "\\" + selectedAppList[i].Text + ".appxbundle";
  253. Thread copyThread;
  254. if (completePath.Length != 0)
  255. {
  256. copyThread = new Thread(() => FileCopyThread(completePath[0], copyToPath));
  257. }
  258. else
  259. {
  260. copyThread = new Thread(() => FileCopyThread(newCompletePath[0], copyToPath));
  261. }
  262. copyThread.Start();
  263. if (!hasCheckingStarted)
  264. {
  265. hasCheckingStarted = true;
  266. Thread threadCheckerThread = new Thread(() => AllThreadsDoneCheckerThread());
  267. threadCheckerThread.Start();
  268. }
  269. }
  270. }
  271. if (selectedAppCount == 0)
  272. {
  273. statusLabel.Visible = false;
  274. errorTitleLabel.Visible = true;
  275. errorDescLabel.Visible = true;
  276. errorDescLabel.Text = "Bitte mindestens eine App auswählen.";
  277. Cursor = Cursors.Default;
  278. }
  279. }
  280.  
  281. public static void FileCopyThread(string copyFrom, string copyTo)
  282. {
  283. try
  284. {
  285. processesDone.Add(false);
  286. File.Copy(copyFrom, copyTo, true);
  287. processesDone.Remove(false);
  288. processesDone.Add(true);
  289. }
  290. catch (IOException e)
  291. {
  292. Console.WriteLine(e.StackTrace);
  293. }
  294. }
  295.  
  296. public static void AllThreadsDoneCheckerThread()
  297. {
  298. Console.WriteLine("Checking for completion...");
  299. bool canProceed = true;
  300. if (selectedAppCount != processesDone.Count)
  301. {
  302. Console.WriteLine("Not all copythreads have been started.");
  303. canProceed = false;
  304. }
  305. foreach (bool b in processesDone)
  306. {
  307. Console.WriteLine(b.ToString());
  308. if (!b)
  309. {
  310. canProceed = false;
  311. }
  312. }
  313. if (!canProceed)
  314. {
  315. canProceed = true;
  316. Thread.Sleep(3000);
  317. Thread threadCheckerThread = new Thread(() => AllThreadsDoneCheckerThread());
  318. threadCheckerThread.Start();
  319. }
  320. else
  321. {
  322. username = Interaction.InputBox("Bitte den Benutzernamen für das Windows Device Portal eingeben.", "Device Portal Anmeldung", "admin", -1, -1);
  323. password = Interaction.InputBox("Bitte das Passwort für das Windows Device Portal eingeben.", "Device Portal Anmeldung", "hololensMMS16", -1, -1);
  324. APImanager.InstallHololensApp();
  325. }
  326. }
  327.  
  328. private void tempComboBox_SelectedIndexChanged(object sender, EventArgs e)
  329. {
  330. ComboBox changedComboBox = (ComboBox)sender;
  331. if (dropdownStates.ContainsKey(changedComboBox.Name))
  332. {
  333. dropdownStates.Remove(changedComboBox.Name);
  334. }
  335. dropdownStates.Add(changedComboBox.Name, changedComboBox.SelectedItem.ToString());
  336. }
  337.  
  338. private void tempComboBoxInitialization(ComboBox box)
  339. {
  340. if (dropdownStates.ContainsKey(box.Name))
  341. {
  342. dropdownStates.Remove(box.Name);
  343. }
  344. dropdownStates.Add(box.Name, box.SelectedItem.ToString());
  345. }
  346.  
  347. private void Form1_Paint(object sender, PaintEventArgs e)
  348. {
  349. Pen pen = new Pen(Color.FromArgb(255, 226, 0, 116));
  350. pen.Width = 5;
  351. e.Graphics.DrawLine(pen, 0, 50, form.Width, 50);
  352. e.Graphics.DrawLine(pen, 0, 5, form.Width, 5);
  353. pen.Dispose();
  354. }
  355. }
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement