/GrinGlobal.InstallHelper/Program.cs

https://gitlab.com/GRIN-Global/GRIN-Global-server · C# · 205 lines · 194 code · 11 blank · 0 comment · 22 complexity · 52d9cce5d60452e754c67a97b176f8a4 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using GrinGlobal.Core;
  7. using System.Diagnostics;
  8. namespace GrinGlobal.InstallHelper {
  9. public class Program {
  10. [STAThread]
  11. public static void Main(string[] args) {
  12. if (args == null || args.Length == 0) {
  13. showUsage();
  14. return;
  15. }
  16. string command = null;
  17. bool waitForExit = false;
  18. bool hideWindow = false;
  19. string program = null;
  20. bool client = false;
  21. List<string> otherArgs = new List<string>();
  22. for(int i=0;i<args.Length;i++){
  23. string a = args[i];
  24. if (i == 0) {
  25. command = a;
  26. continue;
  27. }
  28. switch(a.ToLower()){
  29. case "/w":
  30. case "-w":
  31. case "/wait":
  32. case "-wait":
  33. case "--wait":
  34. waitForExit = true;
  35. break;
  36. case "/hide":
  37. case "-hide":
  38. case "--hide":
  39. hideWindow = true;
  40. break;
  41. case "/showusage":
  42. case "-showusage":
  43. case "--showusage":
  44. case "/?":
  45. case "-?":
  46. case "--?":
  47. case "/help":
  48. case "-help":
  49. case "--help":
  50. showUsage();
  51. return;
  52. case "/client":
  53. case "-client":
  54. case "--client":
  55. client = true;
  56. break;
  57. default:
  58. if (program == null){
  59. if (a.StartsWith("/") || a.StartsWith("-")) {
  60. showUsage();
  61. return;
  62. }
  63. if (a.Contains(" ")){
  64. program = @"""" + a + @"""";
  65. } else {
  66. program = a;
  67. }
  68. } else {
  69. if (a.Contains(" ")){
  70. otherArgs.Add(@"""" + a + @"""");
  71. } else {
  72. otherArgs.Add(a);
  73. }
  74. }
  75. break;
  76. }
  77. }
  78. switch (("" + command).ToLower()) {
  79. case "promptdbengine":
  80. Application.EnableVisualStyles();
  81. var fe = new frmDatabaseEnginePrompt2();
  82. fe.ClientMode = client;
  83. Application.Run(fe);
  84. return;
  85. case "promptdbconn":
  86. Application.EnableVisualStyles();
  87. var fc = new frmDatabaseLoginPrompt();
  88. fc.ClientMode = client;
  89. Application.Run(fc);
  90. return;
  91. case "help":
  92. default:
  93. showUsage();
  94. return;
  95. case "uac":
  96. if (String.IsNullOrEmpty(program)) {
  97. throw new ArgumentException("Name of program to run under UAC elevated privileges must be given.");
  98. }
  99. ProcessStartInfo psi = new ProcessStartInfo(program, String.Join(" ", otherArgs.ToArray()));
  100. psi.CreateNoWindow = hideWindow;
  101. try {
  102. if (program.StartsWith(@"""")) {
  103. program = program.Substring(1);
  104. }
  105. if (program.EndsWith(@"""")) {
  106. program = program.Substring(0, program.Length - 1);
  107. }
  108. psi.UseShellExecute = true;
  109. if (IsVistaOrBetter) {
  110. psi.Verb = "runas";
  111. }
  112. } catch (Exception ex) {
  113. Console.WriteLine("Error: " + ex.Message + " -- Exe=" + psi.FileName + ", Args=" + psi.Arguments);
  114. }
  115. try {
  116. //Console.WriteLine("Hit enter to execute: " + psi.FileName + " " + psi.Arguments + " in directory " + psi.WorkingDirectory + " ... ");
  117. //Console.ReadLine();
  118. Process p = Process.Start(psi);
  119. if (waitForExit) {
  120. p.WaitForExit();
  121. }
  122. } catch (Exception ex2) {
  123. Console.WriteLine("Error: " + ex2.Message + " -- Exe=" + psi.FileName + ", Args=" + psi.Arguments + ", WorkingDirectory=" + psi.WorkingDirectory);
  124. }
  125. return;
  126. }
  127. }
  128. private static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) {
  129. Console.WriteLine(e.Data);
  130. }
  131. private static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) {
  132. Console.WriteLine(e.Data);
  133. }
  134. private static void showUsage() {
  135. Console.WriteLine(@"
  136. Usage: gghelper.exe [command] [commandoptions]
  137. commands:
  138. uac [/wait] [/hide] program [args]
  139. Prompts for elevation via UAC then runs program with elevated privileges.
  140. /wait wait for program to finish before exiting.
  141. /hide suppresses the command box window from displaying
  142. Note: uac command uses Shell Execute to prompt for UAC.
  143. This means output cannot be redirected nor can the working directory
  144. be set. Relative paths may not work properly.
  145. help
  146. Shows this message
  147. promptdbengine [/client]
  148. Displays GUI prompting for database engine
  149. /client Show GUI for choosing a database engine for the Curator Tool
  150. If not specified, shows GUI for choosing a database engine
  151. for the server components
  152. promptdbconn [/db srv] [/winauth (Y|N)] [/dbuser user] [/web srv] [/gguser user]
  153. Displays GUI prompting for database or web service connection info
  154. /db name of database server to show by default
  155. /winauth Y to use windows authentication for db by default, N otherwise
  156. /dbuser name of database user to show by default
  157. /web name of web server to show by default
  158. /gguser GRIN-Global user name to show by default
  159. ");
  160. // /workingdir sets working directory for program
  161. }
  162. private static bool IsVistaOrBetter {
  163. get {
  164. return Environment.OSVersion.Version.Major >= 6
  165. && Environment.OSVersion.Version.Minor >= 0;
  166. }
  167. }
  168. }
  169. }